Results 1 to 3 of 3

Thread: How to send key press events from QGraphicsScene to focused item

  1. #1
    Join Date
    Jul 2007
    Posts
    90
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to send key press events from QGraphicsScene to focused item

    Hi,

    I have a problem in keyPressEvent().

    I have a customized scene which contains n numbers of customized QGraphicsItem.

    I have reimplemented keyPressEvent() in QCustomScene.

    In addition i have also reimplemented keyPressEvent() in my customized item.

    In such situation, only QCustomScene keyPressEvent() is called.

    Is there any way so that i can send keyPressEvent() to currently focused item.

    Regards,
    ~Sanjay

  2. #2
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send key press events from QGraphicsScene to focused item

    I think you should call somewhere to setFocus() to the custom item you want to have focus,also set them as fosusable.I think by default an item gets selected if you left click on it ,(if the item is selectable ,of course) ,but it s never given the focus .
    So if I m right (I m not sure) ,I would try to catch the event after wich you want the item to has focus .Also dont know if after it s necessary to cal clearFocus() ,but I would say it s not necessary if you can be sure that another object will take the keyboar focus .

    Hope it helps .(Actually I hope my answer is not very stupid )

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to send key press events from QGraphicsScene to focused item

    to becom key press events
    void QGraphicsItem::keyPressEvent ( QKeyEvent * event ) [virtual protected]
    This event handler, for event event, can be reimplemented to receive key press events for this item. The default implementation ignores the event. If you reimplement this handler, the event will by default be accepted.
    Calling QEvent::ignore() or QEvent::accept() on event has no effect.
    Note that key events are only received for items that set the ItemIsFocusable flag, and that have keyboard input focus.
    set flag QGraphicsItem::ItemIsFocusable and you having keyPressEvent


    Qt Code:
    1. QGraphicsItem::setFlags(this->flags() | QGraphicsItem::ItemIsFocusable );
    2. setFlag(QGraphicsItem::ItemIsMovable,false);
    To copy to clipboard, switch view to plain text mode 

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.