平台:linux(archlinux)
Qt 版本:4.6.3
这是代码
- 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();
谢谢各位关注!
[ 此帖被xtfllbl在2010-08-18 09:06重新编辑 ]