SetFocus does not set focus to focusable QGraphicsItem
-
wrote on 21 Feb 2015, 20:56 last edited by
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
-
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] -
wrote on 21 Feb 2015, 21:23 last edited by
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)
-
The item is still not visible when you call setFocus
-
wrote on 22 Feb 2015, 03:46 last edited by
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.
-
Painted and visible is not the same thing. Why do you need that look information ?
-
wrote on 23 Feb 2015, 05:58 last edited by
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.
-
Ok but you can't expect an invisible widget to get focus. What would be the problem to show that scene for your test ?
-
wrote on 27 Feb 2015, 10:56 last edited by
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!
-
That's also what happens currently with some of the tests created for Qt
5/10