I just started learning QT in the past week or so and I am an experienced programmer (java, c++, scripting, etc). I've looked through much of the documentation on the graphics view framework including the examples. Since I still did not completely understand, I took the mouse collision demo and started playing with it. What I want to do is restrict the scene to one mouse and be able to control that mouse with the arrow keys.
I'm calling
this->setFlag(ItemIsFocusable,true);
this->setFlag(ItemIsFocusable,true);
To copy to clipboard, switch view to plain text mode
In the constructor of my item. I also thought that I could call
this->setFocus()
this->setFocus()
To copy to clipboard, switch view to plain text mode
to get the focus for that item too - but my item is not yet visible when it is being constructed (it hasn't even been added to the scene yet!).
I got around this by utilizing the timerEvent, calling setFocus in there, and then it will accept keyPressEvent - but I don't want to do that! Thats crazy talk!
What is the correct way to handle this so that I can give my item focus after being added to the scene?
Do I do something like this?
void MyScene
::keyPressEvent(QKeyEvent *event
) {
printf("scene pressed!!!\n");
QList<QGraphicsItem *> myItems = items();
{
item->setFocus();
}
}
void MyScene::keyPressEvent(QKeyEvent *event)
{
printf("scene pressed!!!\n");
QList<QGraphicsItem *> myItems = items();
foreach (QGraphicsItem *item, myItems)
{
item->setFocus();
}
}
To copy to clipboard, switch view to plain text mode
And then somehow pass on the event to my item? Any thoughts would be appreciated!
Bookmarks