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

做网站设计收入b站推广在哪里

做网站设计收入,b站推广在哪里,网站建设贵不贵,做招商加盟的网站很可惜,qt的几个编辑框并没有相关功能。所以我们要自己实现一个。 先讲讲原理: QPlainTextEdit继承自QAbstractScrollArea,编辑发生在其viewport()的边距内。我们可以通过将视口的左边缘设置一个空白区域,…

很可惜,qt的几个编辑框并没有相关功能。所以我们要自己实现一个。

先讲讲原理:
QPlainTextEdit继承自QAbstractScrollArea,编辑发生在其viewport()的边距内。我们可以通过将视口的左边缘设置一个空白区域,用于绘制行号。

之所以使用QPlainTextEdit而不是QTextEdit,因为它针对处理纯文本进行了优化。更重要的是它允许我们高亮多行文字。
 

一.LineNumberArea类

我们新建一个类,叫做LineNumberArea,继承QWidget,并在上绘制行号。

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;
};

二.CodeEditor类

CodeEditor继承QPlainTextEdit,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;
};

当编辑器中的行数发生变化或者编辑器的viewport()滚动时或者编辑器的大小发生时,我们需要调整左侧区域大小,并绘制行号。因此我们需要updateLineNumberWidth()和updateLineNumberArea()方法。

三.CodeEditor类实现

在构造函数中,我们将QPlainTextEdit中的信号连接到对应的槽函数。

然后计算行号区域宽度并高亮显示第一行。

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();//高亮显示
}

lineNumberAreaWidth()函数计算LineNumberArea的宽度并返回。

一般取编辑器最后一行的文字,并计算该行文字的宽度。

按字符’9‘的宽度作为单个字符的宽度。

int CodeEditor::lineNumberAreaWidth()
{int digits = 1;int max = qMax(1, blockCount());//计算数位while (max >= 10) {max /= 10;++digits;}//取字符9的宽度int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;return space;
}

当我们更新行号区域的宽度时,需调用QAbstractScrollArea::setViewportMargins(),更新设置左侧行号区的宽度。

void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
{setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}

当编辑器视口滚动时,会调用updateLineNumberArea。QRect是编辑区域中需要更新(重绘)的部分。dy是鼠标滚动时的距离,单位是像素。

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);
}

每当LineNumberArea接收到绘制事件(paintEvent)时,就会调用CodeEditor的lineNumberAreaPaintEvent。

lineNumberAreaPaintEvent将遍历所有可见的线条,并在每行的额外区域中绘制行号。在纯文本编辑中,每一行都由一个QTextBlock组成;

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::AlignRight, number);}block = block.next();top = bottom;bottom = top + qRound(blockBoundingRect(block).height());++blockNumber;}
}

参考资料:

Code Editor Example | Qt Widgets 5.15.17

http://www.dinnco.com/news/3920.html

相关文章:

  • 推广app赚佣金平台有哪些深圳百度seo整站
  • 网站制作与发布什么是网站推广优化
  • 泰州网站制作公司网站优化基本技巧
  • 网站域名的所有权一站式发稿平台
  • 网站建设公司排名手机百度app安装下载
  • 月夜直播免费完整版seo外链收录
  • 自学网ps教程新手入门百度seo查询系统
  • wordpress.conf优化最狠的手机优化软件
  • 今日要闻10条常州网络推广seo
  • 济宁企业做网站网络推广营销方法
  • 网站的项目建设周期企业管理培训班
  • 长春本地网站制作搜索引擎优化seo专员
  • 北京商地网站建设公司新闻头条最新消息10条
  • 重庆中色十二冶金建设有限公司网站系统优化大师官方下载
  • 中国建设银行官网站查询卡号最新网络营销方式有哪些
  • 安庆做网站赌博代理免费网站建设制作
  • 信阳网站优化中国十大小说网站排名
  • 哈尔滨建工建设集团怎么优化
  • 天门seoseo人员培训
  • .vip域名做网站以下哪个单词表示搜索引擎优化
  • 企业网站开发论文总结服务推广软文
  • 德州哪里有做网站的在线资源链接
  • 网站登录不上去怎么回事软文营销广告案例
  • 网站内容品质提高百度快速排名
  • 网站建设的技术阶段产品推广方式
  • 武汉头条新闻北京网站优化
  • 个人博客网站备案吗博客推广的方法与技巧
  • 园林公司做网站的好处石家庄疫情最新消息
  • 山西太原建站哪家强搜索百度网址网页
  • 如何介绍设计的网站模板全网推广费用