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

常州工厂网站建设萍乡建站公司

常州工厂网站建设,萍乡建站公司,seo专员工资一般多少,赣州企业网Qt QPlainTextEdit高亮显示当前行 文章目录 Qt QPlainTextEdit高亮显示当前行摘要错误的代码正确的代码QTextEdit::ExtraSelection 关键字#xff1a; Qt、 QPlainTextEdit、 QTextBlock、 ExtraSelection、 GPT 摘要 今天要在说一下GPT#xff0c;当下如果你还不会用G… Qt QPlainTextEdit高亮显示当前行 文章目录 Qt QPlainTextEdit高亮显示当前行摘要错误的代码正确的代码QTextEdit::ExtraSelection 关键字 Qt、 QPlainTextEdit、 QTextBlock、 ExtraSelection、 GPT 摘要 今天要在说一下GPT当下如果你还不会用GPT那得推荐你学习一下这个东西可比百度好的的。如果没有GPT那我今天分享的内容还需要在手打一次了。因为我公司没有网代码也没法直接复制粘贴出来。 回到正题今天我们需要 在QPlainTextEdit中实现高亮当前行显示。 错误的代码 在这段代码中我们定义了一个HighlightCurrentLinePlainTextEdit类继承自QPlainTextEdit。我们重写了paintEvent来绘制背景色同时通过cursorPositionChanged信号来触发当前行的高亮。每当光标移动时highlightCurrentLine槽会被调用它会创建一个ExtraSelection来指定当前光标所在行的背景色并将它设置成QPlainTextEdit的extraSelections。 #include QPlainTextEdit #include QPainter #include QTextBlockclass 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() {QListQTextEdit::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 QPlainTextEditclass HighlightCurrentLinePlainTextEdit : public QPlainTextEdit { Q_OBJECTpublic:explicit HighlightCurrentLinePlainTextEdit(QWidget *parent nullptr) {connect(this, QPlainTextEdit::cursorPositionChanged, this, HighlightCurrentLinePlainTextEdit::highlightCurrentLine);// 初始化高亮highlightCurrentLine();}private slots:void highlightCurrentLine() {QListQTextEdit::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 结构有两个主要的成员 QTextCursor cursor这个 QTextCursor 用来指定要高亮显示的文本范围。你可以设置它选择一个词、一行或者文档中的任何段落。 QTextCharFormat format这个 QTextCharFormat 用来指定高亮显示的样式比如背景色、文本颜色、下划线等。 你可以创建一个 ExtraSelection 的列表并通过调用 QTextEdit 的 setExtraSelections() 方法来设置这个列表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(QListQTextEdit::ExtraSelection() selection);在这个例子中QTextEdit 的第11行将被一个蓝色背景和白色前景的样式高亮显示。这种方式不会干扰用户的正常选择而是作为一种额外的视觉提示。
http://www.zqtcl.cn/news/93188/

相关文章:

  • 网站备案一次就可以了吧营销管理培训课程
  • 怎么做网站背景专做民宿预定的网站
  • wordpress安装谷歌分析代码建网站seo
  • 百度外卖网站建设与维护方法建设 银行网网站
  • 小程序开发定制开发上海优化价格
  • 来宾住房和城乡建设局网站做外贸推广要做哪些平台
  • 无锡建设网站制作wordpress 知乎
  • 动漫网站源码免费怎么怎么做网站
  • 和两个黑人同时做网站中工互联网站建设
  • windows10PHP 网站建设app应用分发平台开发
  • 中唯建设工程有限公司网站做网站页面对PS切图
  • 个人网页制作成品欣赏seo网站沙盒期
  • 亚马逊站外推广网站怎么做制作营销网站模板免费下载
  • 加拿大网站后缀设计师互联网
  • 做物流的网站有哪些内容共同建设网站心得
  • 主题资源网站建设什么网站做污水处理药剂的好
  • 河北建设厅网站修改密码在哪58同城宿迁二手房
  • 淘宝联盟的购物网站怎么做免费网站模板素材
  • 淄博市网站云平台长沙seo 优化选智投未来no1
  • 手机网站导航模板wordpress子域名设置
  • 济南市网站推广公司甘肃网站建设方案优化
  • 网站排名西安工商所什么网站可做年报
  • 网站怎样做反向链接哪个网站可以做代码题目
  • opencart做外贸网站怎样丽水市城乡建设局网站
  • 黑色网站配色typora wordpress
  • 哪个网站做的系统好用吗开一家网站建设公司好
  • 高仿服装网站建设高端网站建设服务
  • 网站怎么做前后台存取旅游网站建设的目的与意义是什么意思
  • 网站一年了百度不收录自己做的网站怎么植入erp
  • 怎样做能让招聘网站记住密码广州建设营销型网站