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

福田网站制作报价网站访问量查询工具

福田网站制作报价,网站访问量查询工具,网站建设项目心得体会,python编程软件手机版Qt QPlainTextEdit高亮显示当前行 文章目录 Qt QPlainTextEdit高亮显示当前行摘要错误的代码正确的代码QTextEdit::ExtraSelection 关键字: Qt、 QPlainTextEdit、 QTextBlock、 ExtraSelection、 GPT 摘要 今天要在说一下GPT,当下如果你还不会用G…
头图

Qt QPlainTextEdit高亮显示当前行

文章目录

  • Qt QPlainTextEdit高亮显示当前行
    • 摘要
    • 错误的代码
    • 正确的代码
    • QTextEdit::ExtraSelection

关键字: QtQPlainTextEditQTextBlockExtraSelectionGPT

摘要

今天要在说一下GPT,当下如果你还不会用GPT,那得推荐你学习一下,这个东西可比百度好的的。如果没有GPT,那我今天分享的内容,还需要在手打一次了。因为我公司没有网,代码也没法直接复制粘贴出来。

回到正题,今天我们需要 在QPlainTextEdit中实现高亮当前行显示。

错误的代码

在这段代码中,我们定义了一个HighlightCurrentLinePlainTextEdit类,继承自QPlainTextEdit。我们重写了paintEvent来绘制背景色,同时通过cursorPositionChanged信号来触发当前行的高亮。每当光标移动时,highlightCurrentLine槽会被调用,它会创建一个ExtraSelection来指定当前光标所在行的背景色,并将它设置成QPlainTextEditextraSelections

#include <QPlainTextEdit>
#include <QPainter>
#include <QTextBlock>class HighlightCurrentLinePlainTextEdit : public QPlainTextEdit {Q_OBJECTpublic:HighlightCurrentLinePlainTextEdit(QWidget *parent = nullptr) : QPlainTextEdit(parent) {connect(this, &QPlainTextEdit::cursorPositionChanged,this, &HighlightCurrentLinePlainTextEdit::highlightCurrentLine);highlightCurrentLine();}protected:void paintEvent(QPaintEvent *e) override {QPlainTextEdit::paintEvent(e);QPainter painter(viewport());QRect rect = cursorRect();QRect lineRect = QRect(0, rect.y(), viewport()->width(), rect.height());QColor color = QColor(Qt::yellow).lighter(160);painter.fillRect(lineRect, color);}private slots:void 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);}
};#include "main.moc"

如果按照上面的的写法,你会发现你的文字就不见了,所以还需要改良一下才可以用。

正确的代码

在这段代码中,我们移除了paintEvent方法的覆写,因为这个方法会影响文本的正常显示。我们通过ExtraSelections机制来高亮当前行,它不会干扰文本的显示。

#include <QApplication>
#include <QPlainTextEdit>class HighlightCurrentLinePlainTextEdit : public QPlainTextEdit {
Q_OBJECTpublic:explicit HighlightCurrentLinePlainTextEdit(QWidget *parent = nullptr) {connect(this, &QPlainTextEdit::cursorPositionChanged, this, &HighlightCurrentLinePlainTextEdit::highlightCurrentLine);// 初始化高亮highlightCurrentLine();}private slots:void 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);}
};int main(int argc, char *argv[]) {QApplication a(argc, argv);HighlightCurrentLinePlainTextEdit editor;editor.show();return QApplication::exec();
}#include "main.moc"

QTextEdit::ExtraSelection

QTextEdit::ExtraSelection 是一个在 QTextEdit 控件中用来表示额外的文本选择的结构。在 Qt 框架中,QTextEdit 是一个富文本编辑器控件,它可以用来显示和编辑富文本文档。默认情况下,用户可以在文本编辑器中选择文本,但有时候,开发者可能希望高亮显示文本的其他部分而不影响用户的当前选择。ExtraSelection 提供了这种功能。

ExtraSelection 结构有两个主要的成员:

  1. QTextCursor cursor:这个 QTextCursor 用来指定要高亮显示的文本范围。你可以设置它选择一个词、一行或者文档中的任何段落。

  2. QTextCharFormat format:这个 QTextCharFormat 用来指定高亮显示的样式,比如背景色、文本颜色、下划线等。

你可以创建一个 ExtraSelection 的列表,并通过调用 QTextEditsetExtraSelections() 方法来设置这个列表,QTextEdit 将会绘制出这些额外的选择区域。这通常用于诸如代码高亮、拼写错误提示等场景。

以下是一个 QTextEdit::ExtraSelection 用法的简单例子:

QTextEdit *editor = new QTextEdit;// 创建一个额外的选择
QTextEdit::ExtraSelection selection;// 设置选择范围
selection.cursor = QTextCursor(editor->document()->findBlockByNumber(10)); // 选择第11行(假设行号从0开始)
selection.cursor.select(QTextCursor::LineUnderCursor);// 设置选择的格式(比如,蓝色背景)
selection.format.setBackground(Qt::blue);
selection.format.setForeground(Qt::white);// 应用额外的选择
editor->setExtraSelections(QList<QTextEdit::ExtraSelection>() << selection);

在这个例子中,QTextEdit 的第11行将被一个蓝色背景和白色前景的样式高亮显示。这种方式不会干扰用户的正常选择,而是作为一种额外的视觉提示。


博客签名2021
http://www.zhongyajixie.com/news/56093.html

相关文章:

  • 国外有个专门做病毒营销网站青岛网站权重提升
  • wordpress 目录函数山东搜索引擎优化
  • 做网站哪个地方需要钱关键词歌曲
  • wordpress mp4 插件下载泉州seo托管
  • 个人网站可以备案吗重大新闻事件2023
  • 建设一个网站如何软件网站优化公司
  • 我想成立公司怎么办理seo搜索引擎优化实训总结
  • 政府网站建设的国际今天重大新闻
  • 网站上怎样做超链接驻马店网站seo
  • 如何建造网站视频教程seo全国最好的公司
  • 网页制作app下载seo教程 百度网盘
  • erp软件公司有哪些白山seo
  • 哈尔滨座做网站的官方百度app下载
  • 网站开发工程师和软件工程搜索引擎算法
  • 网站页面设计模板图片网站关键词快速排名服务
  • 展示型网站源码免费发布信息平台有哪些
  • 泉州最好的网站建设公司seo关键词seo排名公司
  • 做公务员题目的网站腾讯会议多少钱一个月
  • 网页设计源代码css案例seo公司运营
  • 少主网络建站深圳建站公司
  • 网站建设用户需求表推广网站推广
  • 有没有免费开网店的软件seo是做什么工作的
  • 选择做网站销售的优势查找网站
  • 网站关键词更改平台app开发制作
  • 建设模板类网站厦门网站关键词推广
  • 网站建设广告合同需要交印花税吗北京网站建设开发公司
  • 网站开发职位介绍seo每日工作
  • 网站建设教程 迅雷下载常用的网络营销方法有哪些
  • 做网站制作怎么样牛推网
  • dede投票类网站源码人民日报新闻