3

我正在 Windows XP 上使用 QGraphicsScene 开发 Qt 4.8 应用程序。当用户双击 QGraphicsTextItem 时,我调用

textItem->setTextInteractionFlags(Qt::TextEditorInteraction);

在下一次选择更改时,我调用

textItem->setTextInteractionFlags(Qt::NoTextInteraction);

这可以正常工作,但我找不到删除编辑中剩余的背景颜色反转的方法。在下面的屏幕截图中,我首先双击第一个文本项并选择字符“2927”。然后我点击第二个测试项目并选择“est”。我没有办法摆脱第一个文本项中仍然倒置的“2927”(尽管它不再处于编辑模式)。

在此处输入图像描述

我也试着打电话:

    textItem->textCursor().clearSelection();
    textItem->update();
    textItem->setTextInteractionFlags(Qt::NoTextInteraction);
    textItem->clearFocus();

但他的行为完全没有改变。

所以现在我找到了一个解决方法:

    QString s = textItem->toPlainText();
    textItem->setPlainText("");
    textItem->setPlainText(s);
    textItem->setTextInteractionFlags(Qt::NoTextInteraction);

我不喜欢那样,但它有效。

任何提示更清洁的解决方案?

4

1 回答 1

5

由于QGraphicsTextItem::textCursor()返回光标的副本,因此您必须将其设置回文本项才能使其生效。

QTextCursor cursor(textItem->textCursor());
cursor.clearSelection();
textItem->setTextCursor(cursor);
于 2012-11-03T05:03:33.437 回答