- if (!currentTextCursor.hasSelection()) {
- currentTextCursor.insertText("**" + tr("Boldface") + "**");
- currentTextCursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 2);
- currentTextCursor.movePosition(QTextCursor::WordLeft, QTextCursor::KeepAnchor, 1);
- currentTextEdit->setTextCursor(currentTextCursor);
- } else {
- currentTextCursor.insertText("**" + currentTextCursor.selectedText() + "**");
- }
问题:
如果光标没有
选中文字,就插入文本**Boldface**,并且选中其中的Boldface,如果
已经选中有文字,则在文字的左右两边分别加上**,问题是这样的
在第一次选中的文字文字之后,当我在QTextEdit中单击后,理论上是选中的
文字被清空,也就是hasSelection()应该返回false,但实际上hasSelection()还是true,并且我用qDebug()测试
了之后,确实还是刚才选中的文字,难道还需要对单击事件做处理吗,不知道表述的清不清楚,请各位不吝指教,谢谢
问题已经解决
,原因是没有更新QTextCursor,也就是说在每次使
用QTextCursor时,需要将现在QTextEdit的光标再传递给QTextCursor,我以前是把QTextCursor当作一个类内全局变
量用了,以为自己会更新的,所以会出现上述问题,也就说,每次使用QTextCursor,需要如下动作:
- QTextCursor currentTextCursor = currentTextEdit->textCursor();
问题:
想用QTextEdit做一个程序执行状态的显示窗口。
设置了readOnly属性。结果有两个问题:
1.在程序执行过程中去翻看前面的
信息,如果不把光标(虽然光标看不见)定位到最后一行,就会从翻看的位置打印信息。有没有把光标定位到最后的API?
解决:
QTextCursor cursor = this->textCursor();
if(!cursor.atEnd())
{
cursor.movePosition(QTextCursor::End,QTextCursor::MoveAnchor,1);
this->setTextCursor(cursor);
}
cursor.insertText(str);