当前位置: 首页 > news >正文

怎样做 网站的快捷链接西安百度竞价托管

怎样做 网站的快捷链接,西安百度竞价托管,上海聚通装修公司地址,展会网站源码文本高亮 对于textedit里录入的部分单词我们可以实现高亮,实现高亮主要依赖于QSyntaxHighlighter。 我们先创建一个Qt Application类,类名MainWindow, 然后新增一个C类,类名为MySyntaxHighlighter。 #ifndef MYSYNTAXHIGHLIGHTER_H #define …

文本高亮

对于textedit里录入的部分单词我们可以实现高亮,实现高亮主要依赖于QSyntaxHighlighter。
我们先创建一个Qt Application类,类名MainWindow, 然后新增一个C++类,类名为MySyntaxHighlighter。

#ifndef MYSYNTAXHIGHLIGHTER_H
#define MYSYNTAXHIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QTextDocument>
class MySyntaxHighlighter:public QSyntaxHighlighter
{Q_OBJECT
public:explicit MySyntaxHighlighter(QTextDocument* parent = 0);//重写实现高亮
protected:void highlightBlock(const QString& text);
};#endif // MYSYNTAXHIGHLIGHTER_H

这个类声明了highlightBlock函数,这是一个虚函数继承自QSyntaxHighlighter。每次我们录入文字时,会自动调用这个函数。下面实现MySyntaxHighlighter类

#include "mysyntaxhighlighter.h"
#include <QFont>MySyntaxHighlighter::MySyntaxHighlighter(QTextDocument* parent):QSyntaxHighlighter (parent)
{}void MySyntaxHighlighter::highlightBlock(const QString &text)
{QTextCharFormat myFormat;myFormat.setFont(QFont("微软雅黑"));myFormat.setFontWeight(QFont::Bold);myFormat.setForeground(Qt::green);//匹配charQString pattern = "\\bchar\\b";//创建正则表达式QRegExp express(pattern);//从索引0的位置开始匹配int index = text.indexOf(express);while (index>0) {int matchLen = express.matchedLength();//对匹配的字符串设置高亮setFormat(index, matchLen, myFormat);index = text.indexOf(express, index+matchLen);}
}

在highlightBlock函数中,我们实现了一个高亮的文字模式,当录入的字符串包含char时,char会被高亮。
我们在mainwindow.ui中添加一个textedit,然后在mainwindow的构造函数中添加我们刚才编写的高亮模块。

MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);m_ligher = new MySyntaxHighlighter(ui->textEdit->document());
}

程序运行后,在编辑器中输入hello char,会看到char高亮了
https://cdn.llfc.club/1665137215851.jpg

实现代码编辑器

Qt 的案例中有提供过文本高亮和显示行号的demo,我把它整理起来了。
我们先声明codeeditor类,以及行号显示的类

#ifndef CODEEDITOR_H
#define CODEEDITOR_H#include <QPlainTextEdit>QT_BEGIN_NAMESPACE
class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;
QT_END_NAMESPACEclass LineNumberArea;class CodeEditor : public QPlainTextEdit
{Q_OBJECTpublic:CodeEditor(QWidget *parent = nullptr);void lineNumberAreaPaintEvent(QPaintEvent *event);int lineNumberAreaWidth();protected:void resizeEvent(QResizeEvent *event) override;private slots:void updateLineNumberAreaWidth(int newBlockCount);void highlightCurrentLine();void updateLineNumberArea(const QRect &rect, int dy);private:QWidget *lineNumberArea;
};class LineNumberArea : public QWidget
{
public:LineNumberArea(CodeEditor *editor) : QWidget(editor), codeEditor(editor){}QSize sizeHint() const override{return QSize(codeEditor->lineNumberAreaWidth(), 0);}protected:void paintEvent(QPaintEvent *event) override{codeEditor->lineNumberAreaPaintEvent(event);}private:CodeEditor *codeEditor;
};#endif

具体代码类的实现

#include "codeeditor.h"#include <QPainter>
#include <QTextBlock>CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
{lineNumberArea = new LineNumberArea(this);connect(this, &CodeEditor::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea);connect(this, &CodeEditor::cursorPositionChanged, this, &CodeEditor::highlightCurrentLine);updateLineNumberAreaWidth(0);highlightCurrentLine();
}int CodeEditor::lineNumberAreaWidth()
{int digits = 1;int max = qMax(1, blockCount());while (max >= 10) {max /= 10;++digits;}int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;return space;
}void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
{setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
{if (dy)lineNumberArea->scroll(0, dy);elselineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());if (rect.contains(viewport()->rect()))updateLineNumberAreaWidth(0);
}void CodeEditor::resizeEvent(QResizeEvent *e)
{QPlainTextEdit::resizeEvent(e);QRect cr = contentsRect();lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}void CodeEditor::highlightCurrentLine()
{QList<QTextEdit::ExtraSelection> extraSelections;if (!isReadOnly()) {QTextEdit::ExtraSelection selection;QColor lineColor = QColor(Qt::yellow).lighter(160);selection.format.setBackground(lineColor);selection.format.setProperty(QTextFormat::FullWidthSelection, true);selection.cursor = textCursor();selection.cursor.clearSelection();extraSelections.append(selection);}setExtraSelections(extraSelections);
}void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{QPainter painter(lineNumberArea);painter.fillRect(event->rect(), Qt::lightGray);QTextBlock block = firstVisibleBlock();int blockNumber = block.blockNumber();int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top());int bottom = top + qRound(blockBoundingRect(block).height());while (block.isValid() && top <= event->rect().bottom()) {if (block.isVisible() && bottom >= event->rect().top()) {QString number = QString::number(blockNumber + 1);painter.setPen(Qt::black);painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),Qt::AlignHCenter, number);}block = block.next();top = bottom;bottom = top + qRound(blockBoundingRect(block).height());++blockNumber;}
}

上面实现了代码编辑器的行号和当前行黄色高亮显示。
接下来实现高亮显示类

#ifndef HIGHLIGHTER_H
#define HIGHLIGHTER_H#include <QSyntaxHighlighter>
#include <QTextCharFormat>
#include <QRegularExpression>QT_BEGIN_NAMESPACE
class QTextDocument;
QT_END_NAMESPACE//! [0]
class Highlighter : public QSyntaxHighlighter
{Q_OBJECTpublic:Highlighter(QTextDocument *parent = 0);protected:void highlightBlock(const QString &text) override;private:struct HighlightingRule{QRegularExpression pattern;QTextCharFormat format;};QVector<HighlightingRule> highlightingRules;QRegularExpression commentStartExpression;QRegularExpression commentEndExpression;QTextCharFormat keywordFormat;QTextCharFormat classFormat;QTextCharFormat singleLineCommentFormat;QTextCharFormat multiLineCommentFormat;QTextCharFormat quotationFormat;QTextCharFormat functionFormat;
};
//! [0]#endif // HIGHLIGHTER_H

具体实现细节如下,先定义高亮的正则规则,然后在highlightBlock函数里根据规则点亮不同的单词

#include "highlighter.h"//! [0]
Highlighter::Highlighter(QTextDocument *parent): QSyntaxHighlighter(parent)
{HighlightingRule rule;keywordFormat.setForeground(Qt::darkBlue);keywordFormat.setFontWeight(QFont::Bold);const QString keywordPatterns[] = {QStringLiteral("\\bchar\\b"), QStringLiteral("\\bclass\\b"), QStringLiteral("\\bconst\\b"),QStringLiteral("\\bdouble\\b"), QStringLiteral("\\benum\\b"), QStringLiteral("\\bexplicit\\b"),QStringLiteral("\\bfriend\\b"), QStringLiteral("\\binline\\b"), QStringLiteral("\\bint\\b"),QStringLiteral("\\blong\\b"), QStringLiteral("\\bnamespace\\b"), QStringLiteral("\\boperator\\b"),QStringLiteral("\\bprivate\\b"), QStringLiteral("\\bprotected\\b"), QStringLiteral("\\bpublic\\b"),QStringLiteral("\\bshort\\b"), QStringLiteral("\\bsignals\\b"), QStringLiteral("\\bsigned\\b"),QStringLiteral("\\bslots\\b"), QStringLiteral("\\bstatic\\b"), QStringLiteral("\\bstruct\\b"),QStringLiteral("\\btemplate\\b"), QStringLiteral("\\btypedef\\b"), QStringLiteral("\\btypename\\b"),QStringLiteral("\\bunion\\b"), QStringLiteral("\\bunsigned\\b"), QStringLiteral("\\bvirtual\\b"),QStringLiteral("\\bvoid\\b"), QStringLiteral("\\bvolatile\\b"), QStringLiteral("\\bbool\\b")};for (const QString &pattern : keywordPatterns) {rule.pattern = QRegularExpression(pattern);rule.format = keywordFormat;highlightingRules.append(rule);
//! [0] //! [1]}
//! [1]//! [2]classFormat.setFontWeight(QFont::Bold);classFormat.setForeground(Qt::darkMagenta);rule.pattern = QRegularExpression(QStringLiteral("\\bQ[A-Za-z]+\\b"));rule.format = classFormat;highlightingRules.append(rule);
//! [2]//! [3]singleLineCommentFormat.setForeground(Qt::red);rule.pattern = QRegularExpression(QStringLiteral("//[^\n]*"));rule.format = singleLineCommentFormat;highlightingRules.append(rule);multiLineCommentFormat.setForeground(Qt::red);
//! [3]//! [4]quotationFormat.setForeground(Qt::darkGreen);rule.pattern = QRegularExpression(QStringLiteral("\".*\""));rule.format = quotationFormat;highlightingRules.append(rule);
//! [4]//! [5]functionFormat.setFontItalic(true);functionFormat.setForeground(Qt::blue);rule.pattern = QRegularExpression(QStringLiteral("\\b[A-Za-z0-9_]+(?=\\()"));rule.format = functionFormat;highlightingRules.append(rule);
//! [5]//! [6]commentStartExpression = QRegularExpression(QStringLiteral(" /\\*"));commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));
}
//! [6]//! [7]
void Highlighter::highlightBlock(const QString &text)
{for (const HighlightingRule &rule : qAsConst(highlightingRules)) {QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);while (matchIterator.hasNext()) {QRegularExpressionMatch match = matchIterator.next();setFormat(match.capturedStart(), match.capturedLength(), rule.format);}}
//! [7] //! [8]setCurrentBlockState(0);
//! [8]//! [9]int startIndex = 0;if (previousBlockState() != 1)startIndex = text.indexOf(commentStartExpression);//! [9] //! [10]while (startIndex >= 0) {
//! [10] //! [11]QRegularExpressionMatch match = commentEndExpression.match(text, startIndex);int endIndex = match.capturedStart();int commentLength = 0;if (endIndex == -1) {setCurrentBlockState(1);commentLength = text.length() - startIndex;} else {commentLength = endIndex - startIndex+ match.capturedLength();}setFormat(startIndex, commentLength, multiLineCommentFormat);startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);}
}
//! [11]

接下来在MainWindow里添加editor

void MainWindow::setupEditor()
{QFont font;font.setFamily("Courier");font.setFixedPitch(true);font.setPointSize(10);editor = new CodeEditor();editor->setFont(font);highlighter = new Highlighter(editor->document());QFile file("mainwindow.h");if (file.open(QFile::ReadOnly | QFile::Text))editor->setPlainText(file.readAll());
}

运行程序后,输入部分代码显示如下
https://cdn.llfc.club/1665137935233.jpg
具体细节大家可以参考代码理解即可。

总结

源码链接https://gitee.com/secondtonone1/qt-learning-notes


文章转载自:
http://dinncoelectrothermics.tpps.cn
http://dinncotachinid.tpps.cn
http://dinncobva.tpps.cn
http://dinncoperverted.tpps.cn
http://dinncocopita.tpps.cn
http://dinncosphagnum.tpps.cn
http://dinncorima.tpps.cn
http://dinncofogfruit.tpps.cn
http://dinncoleptoprosopic.tpps.cn
http://dinncohurly.tpps.cn
http://dinncoshamefacedly.tpps.cn
http://dinncocleveite.tpps.cn
http://dinncoosteotome.tpps.cn
http://dinncononcountry.tpps.cn
http://dinncomuck.tpps.cn
http://dinncoaggro.tpps.cn
http://dinncowilbur.tpps.cn
http://dinncodemerol.tpps.cn
http://dinncodangle.tpps.cn
http://dinncothievishly.tpps.cn
http://dinncopiece.tpps.cn
http://dinncothriftless.tpps.cn
http://dinncotelescopist.tpps.cn
http://dinncooverblouse.tpps.cn
http://dinncooctahedron.tpps.cn
http://dinncopanzer.tpps.cn
http://dinncoversicle.tpps.cn
http://dinncopollinical.tpps.cn
http://dinncoungoverned.tpps.cn
http://dinncoblesbok.tpps.cn
http://dinncosalesite.tpps.cn
http://dinnconongonococal.tpps.cn
http://dinncocolubrid.tpps.cn
http://dinncocomplexity.tpps.cn
http://dinncowashingtonite.tpps.cn
http://dinncovisualist.tpps.cn
http://dinncoapotheosis.tpps.cn
http://dinncocutover.tpps.cn
http://dinncoflocculose.tpps.cn
http://dinncoapologist.tpps.cn
http://dinncoharmonious.tpps.cn
http://dinncoivba.tpps.cn
http://dinncowardship.tpps.cn
http://dinncofrigging.tpps.cn
http://dinncosia.tpps.cn
http://dinncorubescent.tpps.cn
http://dinncopuddling.tpps.cn
http://dinncochemosmosis.tpps.cn
http://dinncohomonym.tpps.cn
http://dinncotoxigenesis.tpps.cn
http://dinncosinuatrial.tpps.cn
http://dinncotessitura.tpps.cn
http://dinnconoil.tpps.cn
http://dinncoadperson.tpps.cn
http://dinncoastrogeology.tpps.cn
http://dinncoquenton.tpps.cn
http://dinncotig.tpps.cn
http://dinncopsychologise.tpps.cn
http://dinncoborrow.tpps.cn
http://dinnconumeral.tpps.cn
http://dinncocomparable.tpps.cn
http://dinncoiou.tpps.cn
http://dinncotakovite.tpps.cn
http://dinncoxsl.tpps.cn
http://dinncofrit.tpps.cn
http://dinncognaw.tpps.cn
http://dinncocongealer.tpps.cn
http://dinncosolvate.tpps.cn
http://dinncosunfish.tpps.cn
http://dinncohominization.tpps.cn
http://dinncounspiked.tpps.cn
http://dinncopaternoster.tpps.cn
http://dinncointerstitial.tpps.cn
http://dinncogristmill.tpps.cn
http://dinncobeachbound.tpps.cn
http://dinncowomanhood.tpps.cn
http://dinncoprotoxylem.tpps.cn
http://dinncocommonalty.tpps.cn
http://dinncotiling.tpps.cn
http://dinncoboccia.tpps.cn
http://dinncouredium.tpps.cn
http://dinncobrindled.tpps.cn
http://dinncolumbaginous.tpps.cn
http://dinncophyllotaxy.tpps.cn
http://dinncoparallelveined.tpps.cn
http://dinncocoarctation.tpps.cn
http://dinncocommercioganic.tpps.cn
http://dinncoeatage.tpps.cn
http://dinncosonuvabitch.tpps.cn
http://dinncofluorometer.tpps.cn
http://dinncoexperimenter.tpps.cn
http://dinncocolonic.tpps.cn
http://dinncocarrion.tpps.cn
http://dinncofaradic.tpps.cn
http://dinncolipizzaner.tpps.cn
http://dinncocatalyze.tpps.cn
http://dinncohandball.tpps.cn
http://dinncobatch.tpps.cn
http://dinncosandglass.tpps.cn
http://dinncogyro.tpps.cn
http://www.dinnco.com/news/135796.html

相关文章:

  • 综合网站建设网络营销策略理论有哪些
  • 有关师德建设的网站网址怎么创建
  • 做风险代理案源的网站济南头条今日新闻
  • 网站内部链接的作用有哪些全媒体运营师培训费用
  • 做策划有帮助的网站百度联盟推广
  • 网站建设询价单新东方烹饪培训学校
  • 怎么做cms网站广州百度推广开户
  • 江西省网站备案2020十大网络热词
  • wordpress8小时泰州seo网站推广
  • 行政审批局政务服务网站建设情况从事网络营销的公司
  • 努力把网站建设成为发外链平台
  • 分类网站上怎么做锚文本淘宝seo排名优化
  • 没有工信部备案的网站是骗子吗西安百度百科
  • 免费网站下载直播软件免费百度提升优化
  • vs网站开发参考文献网页浏览器
  • 红色旅游网页设计郑州百度快照优化
  • 百度推广怎么做网站seo实战技术培训
  • 徐州网站建设 网站制作seo关键词排名优化怎样收费
  • 搜索引擎如何找到网站友情链接是免费的吗
  • wordpress数据库编码seo优化师是什么
  • 网站建设公司哪家强seo管理是什么
  • 南部网站建设项目外包平台
  • wordpress 文章页面怎样全屏显示南京百度推广优化
  • 提供给他人做视频解析的网站源码在线看网址不收费不登录
  • 安装wordpress报错seo刷排名软件
  • 小型营销企业网站建设策划app推广渠道在哪接的单子
  • 网站开发 经常要清理缓存潍坊百度快速排名优化
  • 网站文章怎么做标签排名轻松seo 网站推广
  • 北京做网站的大公司有哪些网站制作厂家有哪些
  • 徐州市贾汪区建设局网站燃灯seo