如何突出显示一个QTextEdit中的文本字符串 [英] How to highlight a string of text within a QTextEdit

查看:880
本文介绍了如何突出显示一个QTextEdit中的文本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名学生程序员,目前正在使用Qt4开发工作应用程序。我正在构建一个公式编辑器,我有问题尝试突出显示我的 QTextEdit 字段。我有一个函数,通过解析QTextEdit字符串,并返回一个开始和结束整数的错误所在的位置。我原来的策略是在这两个点使用HTML标签来突出显示错误。不幸的是,似乎有一个html标记和方程语法的问题。

I'm a student programmer currently developing an application for work using Qt4. I am building an equation editor and I'm having issues attempting to highlight a string within my QTextEdit field. I have a function that parses through the QTextEdit string and returns an a start and end integer of where an error is located. My original strategy was to use HTML tags at these two points to highlight the error. Unfortunately there appears to be an issue with html tagging and the equation syntax.

我认为我需要的是一个策略,它依赖于Qt的库来设置这两个索引之间的背景颜色。我开始查找 QSyntaxHighlighter ;然而我认为这更多是为了突出使用预定义的一组法律,而不是只是抓住一个& b并设置背景颜色。如果我可以使用语法荧光笔请提供我和示例或参考,因为我已经阅读过的文档,没有找到任何东西。

What I think I need is a strategy that relies on Qt's library to set a background color between these two indices. I began looking a QSyntaxHighlighter; however I think that this is more for highlighting using a predefined set of laws and not for just grabbing up anything between a & b and setting the background color. If I can use syntax highlighter please provide me with and example or reference as I have already read through the documentation and didn't find anything.

感谢您提供任何帮助!

Thanks for any help in advance!

只是为了强调html兼容性问题; html变得有问题,和>使用的符号。

P.S. Just to emphasize on the html compatibility issues; html becomes problematic due to multiple < and > signs used.

推荐答案

您可以使用 QTextCursor QTextCharFormat

QTextEdit *edit = new QTextEdit;
...
int begin = ...
int end = ...
...

QTextCharFormat fmt;
fmt.setBackground(Qt::yellow);

QTextCursor cursor(edit->document());
cursor.setPosition(begin, QTextCursor::MoveAnchor);
cursor.setPosition(end, QTextCursor::KeepAnchor);
cursor.setCharFormat(fmt);

这篇关于如何突出显示一个QTextEdit中的文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆