Qt中关于QTextEdit的问题,如何获取选中其中某个位置数据呢?

2025-03-04 09:39:39
推荐回答(1个)
回答1:

可以使用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);

这是我从stackoverflow上搬来了。。。how to highlight a string of text within a qtextedit