Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SetFocus does not set focus to focusable QGraphicsItem
Forum Updated to NodeBB v4.3 + New Features

SetFocus does not set focus to focusable QGraphicsItem

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    richelbilderbeek
    wrote on last edited by
    #1

    It is known that SetFocus on a QGraphicsItem will fail if it is not focusable ( e.g. http://permalink.gmane.org/gmane.comp.lib.qt.general/19804 ), which some are by default.

    When I use SetFocus on a focusable QGraphicsRectItem that is member of a scene and has been visible, hasFocus still returns false:

    @
    QGraphicsRectItem item;
    assert(item.flags() && QGraphicsItem::ItemIsFocusable);
    assert(item.scene());
    assert(item.isVisible());

    assert(!item.hasFocus()); //No focus yet

    item.setFocus(); //Set focus

    //View item, force painting of item to image, just to get it work

    assert(item.hasFocus()); //Why does this fail?
    @

    I am using Qt Creator 3.1.1 based on Qt 5.3.0 using GCC 4.9.1 (the 32 bit version).

    A full compilable example:

    @
    #include <cassert>
    #include <QApplication>
    #include <QGraphicsView>
    #include <QGraphicsRectItem>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    QGraphicsScene scene;
    QGraphicsView view(&scene);
    view.setGeometry(100,100,400,100);

    QGraphicsRectItem item;
    item.setFlags(
    QGraphicsItem::ItemIsSelectable
    | QGraphicsItem::ItemIsFocusable
    );

    item.setRect(-20,-10,40,20);

    scene.addItem(&item);

    assert(item.flags() && QGraphicsItem::ItemIsSelectable);
    assert(item.flags() && QGraphicsItem::ItemIsFocusable);
    assert(item.scene());
    assert(item.isVisible());

    assert(!item.hasFocus()); //No focus yet

    item.setFocus(); //Set focus

    view.show(); //Show it

    //Force paint of item
    {
    QImage image(scene.sceneRect().size().toSize(), QImage::Format_ARGB32);
    image.fill(Qt::transparent);
    QPainter painter(&image);
    scene.render(&painter);
    image.save("tmp.png");
    }

    assert(item.hasFocus()); //Why does this fail?

    return a.exec();
    }
    @

    Thanks for your suggestions, Richel Bilderbeek

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Following's setFocus documentation, it seems that you are in the case described here:
      [quote]If this item is not visible, not active, or not associated with a scene, it will not gain immediate input focus. However, it will be registered as the preferred focus item for its subtree of items, should it later become visible.
      [/quote]

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        richelbilderbeek
        wrote on last edited by
        #3

        Ah, excellent reply! In the full example, I do make it an item of a QGraphicsScene. So what could it else be? (note: I modified it in the original code snippet)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The item is still not visible when you call setFocus

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            richelbilderbeek
            wrote on last edited by
            #5

            Even when I force the item to be painted to an image, it never gets focus as well (I added this to the code). Perhaps a QGraphicsItem can only get focus when shown on screen, yet I want to avoid this: the ultimate goal is to test if a (derived class of) QGraphicsItem looks different with or without focus.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Painted and visible is not the same thing. Why do you need that look information ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • R Offline
                R Offline
                richelbilderbeek
                wrote on last edited by
                #7

                I want to test that a focused QGraphicsItem is drawn correctly for debugging purposes. For example, it should be different than an unfocused QGraphicsItem, as it might have a dashed edge.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Ok but you can't expect an invisible widget to get focus. What would be the problem to show that scene for your test ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    richelbilderbeek
                    wrote on last edited by
                    #9

                    I want to do some basic debugging tests, for example, to check that an item with focus is drawn differently than an item without focus.

                    Assuming you are right, I guess the tester will have to deal with some screens that pop up very shortly.

                    Thanks for the help!

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      That's also what happens currently with some of the tests created for Qt

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved