Results 1 to 2 of 2

Thread: capture key press and release event in QGraphicsScene subclass

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default capture key press and release event in QGraphicsScene subclass

    Hello forum


    I have the following class memeber functions that tries to capture the key press and release events, but none of them is captured.


    Qt Code:
    1. class DiagramScene : public QGraphicsScene
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. enum Mode { InsertItem, InsertLine, InsertText, MoveItem };
    7.  
    8. DiagramScene(QMenu *itemMenu, QObject *parent = 0);
    9. QFont font() const
    10. { return myFont; }
    11. QColor textColor() const
    12. { return myTextColor; }
    13. QColor itemColor() const
    14. { return myItemColor; }
    15. QColor lineColor() const
    16. { return myLineColor; }
    17. void setLineColor(const QColor &color);
    18. void setTextColor(const QColor &color);
    19. void setItemColor(const QColor &color);
    20. void setFont(const QFont &font);
    21.  
    22. public slots:
    23. void setMode(Mode mode);
    24. void setItemType(DiagramItem::DiagramType type);
    25. void editorLostFocus(DiagramTextItem *item);
    26.  
    27. signals:
    28. void itemInserted(DiagramItem *item);
    29. void textInserted(QGraphicsTextItem *item);
    30. void itemSelected(QGraphicsItem *item);
    31.  
    32. protected:
    33. void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    34. void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
    35. void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
    36.  
    37. void keyReleaseEvent(QKeyEvent *event);
    38. void keyPressEvent(QKeyEvent *event);
    39.  
    40. private:
    41. bool isItemChange(int type);
    42.  
    43. DiagramItem::DiagramType myItemType;
    44. QMenu *myItemMenu;
    45. Mode myMode;
    46. bool leftButtonDown;
    47. QPointF startPoint;
    48.  
    49.  
    50. QFont myFont;
    51. DiagramTextItem *textItem;
    52. QColor myTextColor;
    53. QColor myItemColor;
    54. QColor myLineColor;
    55. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void DiagramScene::keyPressEvent(QKeyEvent *event)
    2. {
    3. if(event->key() == Qt::Key_Control)
    4. {
    5. std::cout << "Ctrl pressed" << endl;
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 


    Any hint on this?


    Regards
    Sajjad

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: capture key press and release event in QGraphicsScene subclass

    The QGraphicsScene eats the events before they get to your handler. Try implementing these handlers on the QGraphicsView instead.

Similar Threads

  1. Replies: 1
    Last Post: 5th September 2012, 03:22
  2. Replies: 5
    Last Post: 7th May 2011, 21:56
  3. How catch key press and key release for entire application
    By hubbobubbo in forum Qt Programming
    Replies: 4
    Last Post: 1st June 2010, 20:53
  4. Paint event function in key press event
    By soumya in forum Qt Programming
    Replies: 6
    Last Post: 2nd February 2010, 12:40
  5. Mouse press event in a QGraphicsScene
    By Lykurg in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 10:28

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.