如何从 Qt QGraphicsTextItem 中删除选择绘图

RED*_*AIR 3 windows qt selection qgraphicsscene

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

textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
Run Code Online (Sandbox Code Playgroud)

在下一次选择更改时我打电话

textItem->setTextInteractionFlags(Qt::NoTextInteraction);
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

我也尝试打电话:

    textItem->textCursor().clearSelection();
    textItem->update();
    textItem->setTextInteractionFlags(Qt::NoTextInteraction);
    textItem->clearFocus();
Run Code Online (Sandbox Code Playgroud)

但他的行为根本没有改变。

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

    QString s = textItem->toPlainText();
    textItem->setPlainText("");
    textItem->setPlainText(s);
    textItem->setTextInteractionFlags(Qt::NoTextInteraction);
Run Code Online (Sandbox Code Playgroud)

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

有什么更清洁的解决方案的提示吗?

Arn*_*nce 5

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

QTextCursor cursor(textItem->textCursor());
cursor.clearSelection();
textItem->setTextCursor(cursor);
Run Code Online (Sandbox Code Playgroud)