Important: Please read the Qt Code of Conduct -

[Completely solved] QGraphicsTextItem: Howto start editing via code?

  • How do I start text editing via code for those QTextDocument based classes?

    Based on my experiments with QGraphicsTextEdit, the obvious attempt was to use setFocus. However, while I get the nice frame that indicates editing, and also a blinking cursor, I cannot type until I actually click into the widget using the mouse.

    How to do it? Of course, I can simulate a mouse click, but that sounds ugly.

    EDIT: I have since found that in a very simple test sample, setFocus works. So something in my environment seems to block me.

    EDIT2: Apparently, a QPushButton is soaking up the key press events. How I just have to find out why it's doing that, although I clearly have set the focus elsewhere.

    EDIT3: Setting the focus on a QGraphicsItem does not automatically cause it to receive keyboard events, because one of the QGraphicsViews showing this item must receive the keyboard focus, too.

  • have you tried this:

    @QWidget *w = QApplication::focusWidget();
    if(w) w->clearFocus();
    // set your focus


    bq. Returns the application widget that has the keyboard input focus, or 0 if no widget in this application has the focus.

  • No, I haven't. But it now appears logical to me that a QGraphicsItem cannot know which of the views displaying it should receive the focus. Therefore, setFocus on a QGraphicsItem alone does not contain enough information to determine the focusWidget.

Log in to reply