3

I am developing a Qt 4.8 application using QGraphicsScene on Windows XP. When the user double clicks on a QGraphicsTextItem, i call

textItem->setTextInteractionFlags(Qt::TextEditorInteraction);

At the next selection change i call

textItem->setTextInteractionFlags(Qt::NoTextInteraction);

This works correctly, but i find no way to remove the inversion of the background color that remains from editing. In the screen shot below, i first double clicked on the first text item and selected the characters "2927". Then i clicked on the second test item and selected "est". I find no way to get rid of the still inverted "2927" in the first text item (although its not in edit mode anymore).

enter image description here

I also tried to call:

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

But his does not at all change the behavior.

So now i found a workaround:

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

I dont like that, but it works.

Any hint for a cleaner solution?

RED SOFT ADAIR
  • 12,032
  • 10
  • 54
  • 92

1 Answers1

5

Since QGraphicsTextItem::textCursor() returns a copy of the cursor, you have to set it back to the text item for it to have any effect.

QTextCursor cursor(textItem->textCursor());
cursor.clearSelection();
textItem->setTextCursor(cursor);
Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
  • Sorry, missed that! Strange that it doesn't work. I was going to suggest making sure that the call to `clearSelection()` is actually being executed but if your workaround is working I guess it's likely the other code was being called correctly. Perhaps I'll try an experiment on this end. – Arnold Spence Nov 03 '12 at 20:37