RED SOFT ADAIR
RED SOFT ADAIR

Reputation: 12218

How do i remove the selection drawing from a Qt QGraphicsTextItem

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?

Upvotes: 3

Views: 821

Answers (1)

Arnold Spence
Arnold Spence

Reputation: 22272

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);

Upvotes: 5

Related Questions