0

Given a QTextBlock retrieved from QPlainTextEdit, I want to change background of the text in that block. I know how to do this with the help of textCursor() but in this case textCursor could be somewhere else. I am traversing through the file text line by line and would like to change background of the current line irrespective of the cursor position. Please let me know if it can be done.

4

1 回答 1

1

将光标移动到要标记的行有什么问题?如果需要,您可以保存旧的光标位置。

int oldPos = edit->textCursor().position();

QTextCursor cursor = edit->textCursor();
int oldPos = cursor.position();

int linePos = // get the line position

cursor.setPosition(linePos);
cursor.select(QTextCursor::LineUnderCursor);
cursor.setCharFormat(format);

cursor.setPosition(oldPos);
edit->setTextCursor(cursor);
于 2013-03-04T07:16:39.107 回答