Results 1 to 3 of 3

Thread: QGraphicsItem and focus for key input

  1. #1

    Default QGraphicsItem and focus for key input

    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

    Qt Code:
    1. 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

    Qt Code:
    1. 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?

    Qt Code:
    1. void MyScene::keyPressEvent(QKeyEvent *event)
    2. {
    3. printf("scene pressed!!!\n");
    4. QList<QGraphicsItem *> myItems = items();
    5. foreach (QGraphicsItem *item, myItems)
    6. {
    7. item->setFocus();
    8. }
    9. }
    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!

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem and focus for key input

    Setting focus in the constructor doesnt make sense... all mouse cannot have focus..

    so only the last one might have focus. A mouse will also have focus when mouse is clicked on it..

    u need to override the keyPressEvent of the Mouse,,, not scene...
    just check the attachment... I am able to change the po of the mouse on key press

    also if u uncomment setFlags line in constuctor, u will be able to get focus on the mouse u click.. happy tweaking
    Attached Files Attached Files

  3. #3

    Default Re: QGraphicsItem and focus for key input

    Excellent example. Thanks so much!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.