当前位置: 首页 > 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://www.ritt.cn/news/11046.html

相关文章:

  • 珠海微信网站一个产品的宣传和推广方案
  • 微信的网站开发搜索 引擎优化
  • wordpress+并发量郑州seo技术服务
  • 深圳做网站找谁免费找客户软件
  • 文章页模板wordpress邯郸seo优化公司
  • 免费注册b2b网站网站排名优化
  • WordPress文章资讯主题怎么做seo网站关键词优化
  • b2b和b2c的区别和联系佛山seo网站排名
  • 网站设置怎么调合肥seo代理商
  • 网站建设公司面临的问题百度竞价关键词出价技巧
  • 成都网站代运营web网页模板
  • 闸北区网站建设网页制网址大全浏览器主页
  • 赣州市做网站西安百度提升优化
  • 怎么做网站或APP软文模板300字
  • 大家都在哪些网站做宣传广东省最新新闻
  • 8469网站南宁seo推广外包
  • 电商网站开发公司杭州促销活动推广语言
  • 通辽网站建设0475seo成都计算机培训机构排名前十
  • 最专业的网站建设seo1搬到哪里去了
  • 网站建设自己怎么做百度官方版下载
  • wordpress修改固定连接插件苏州seo关键词优化软件
  • 把自己的电脑做网站服务器黑五类广告推广
  • 制作网页常用的软件有哪些优质的seo快速排名优化
  • 织梦网站需要付费吗地域名网址查询
  • 购物中心网站建设seo具体优化流程
  • 代刷网站怎么做免费推广的网站
  • 商品网站建设方案小辉seo
  • 电子商务网站建设费用最新的即时比分
  • 网站建设手机版模板市场调研表模板
  • 福永做网站的公司外贸建站与推广