[SOLVED] Make the cursor visible in QGraphicsTextItem



  • Hello.

    I have multiple QGraphicsTextItems in a QGraphicsScene, and I would like the cursor to be visible in the focused text item. When I edit the text in one of these items the cursor becomes visible, but when a new item is focused (because I clicked on it, or because setFocus() is called) the cursor stays invisible until the next edit. Also the dashed frame around a newly focused item is not drawn correctly, so maybe this is an drawing issue. Calling update() in the QGraphicsTextItem's focusInEvent did not change anything, though.

    Can somebody help me with this?



  • I'm sorry to raise this question again. I tried even to insert some Text by calling textCursor().inserText("a"), or to call document()->undo(); document()->redo() in focusInEvent (because both these actions triggered by the user make the cursor appear), but neither works. Is there really no way to make the cursor visible?



  • I think you are talking about QTextCursor. Anyway:

    On foucusOutEvent:
    @
    QTextCursor textCursor = textItem->textCursor();
    textCursor.clearSelection();
    textItem->setTextCursor(textCursor);
    @



  • Thanks for your response. I did try your code, but it did not change anything. But that's not surprising, as there is no selection in the first place. The QGraphicsTextItem has the correct cursor set, but when the program is running the cursor is not visible until I type something. After that there is a vertical line (the I-beam cursor) visible, but it does not appear immediately when the QGraphicsTextItem receives the focus. I find it very irritating to start entering text when I cannot see where the cursor is, so I would like to make it visible before the first user interaction.



  • mouseDoubleClickEvent
    @
    textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
    textItem->setFocus();
    textItem->setCursor(QCursor(Qt::IBeamCursor));
    @

    focusOutEvent
    @
    textItem->setTextInteractionFlags(Qt::NoTextInteraction);
    textItem->unsetCursor();
    @

    And if You are playing with diagramscene: (When you creates an QGraphicsTextItem you need to set the cursor)
    @
    //! [7] //! [8]
    case InsertText:
    .....
    textItem->setCursor(QCursor(Qt::IBeamCursor));
    @

    And do not forget this line at the end of focusInEvent:
    @
    QGraphicsTextItem::focusInEvent(event);
    @

    If the problem continues, please post a bit of your code.



  • Thank you so much! I'm feeling very stupid now...
    I did indeed forget to call
    @QGraphicsTextItem::focusInEvent(event);@

    If I had posted some code earlier someone probably would have noticed. Anyway, thank you for taking the time to help me!



  • You're welcome (:


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.