Results 1 to 2 of 2

Thread: event handling...

  1. #1
    Join Date
    Mar 2008
    Posts
    15
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default event handling...

    I have a simple class inherits QGraphicsRectItem class. I want it to write "testing" in the mousePressEvent() but it doesn't...where am i doing wrong?i examined the tutorials but couldn't find my fault...

    Qt Code:
    1. QGraphicsScene scene(0,0,200,200);
    2. scene.addItem(new MyItem(0));
    3. QGraphicsView view(&scene);
    4. view.show();
    5. return a.exec();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef MYITEM_H_
    2. #define MYITEM_H_
    3. #include <QGraphicsItem>
    4. class MyItem:public QGraphicsRectItem
    5. {
    6. public:
    7. MyItem(QGraphicsItem* parent=0,QGraphicsScene *s=0);
    8. virtual ~MyItem();
    9.  
    10. QRectF boundingRect() const;
    11. protected:
    12. void mousePressEvent(QGraphicsSceneMouseEvent *e);
    13. void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );
    14.  
    15. };
    16.  
    17. #endif /*MYITEM_H_*/
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "MyItem.h"
    2. #include <QtGui>
    3. MyItem::MyItem(QGraphicsItem* p,QGraphicsScene *s):
    4. {
    5.  
    6. }
    7.  
    8. MyItem::~MyItem()
    9. {
    10. }
    11.  
    12. void MyItem::mousePressEvent(QGraphicsSceneMouseEvent* e){
    13. qDebug("testing");
    14. QGraphicsRectItem::mousePressEvent(e);
    15.  
    16. }
    17.  
    18. void MyItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
    19. {
    20. painter->fillRect(10,10,50,50,QBrush(Qt::red,Qt::SolidPattern));
    21. }
    22.  
    23. QRectF MyItem::boundingRect() const
    24. {
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: event handling...

    You need to make your item selectable. Add the following line to the MyItem constructor:
    Qt Code:
    1. setFlags(QGraphicsItem::ItemIsSelectable);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Qt event queue overloading?
    By gct in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2008, 19:39
  2. Replies: 1
    Last Post: 16th October 2007, 23:41
  3. QGraphicsView Handling Child Event
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 01:32
  4. The event fired by the mouse click on the frame
    By Placido Currò in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2007, 10:05
  5. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 22:55

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.