PDA

View Full Version : event handling...



xyzt
24th March 2008, 00:37
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...



QGraphicsScene scene(0,0,200,200);
scene.addItem(new MyItem(0));
QGraphicsView view(&scene);
view.show();
return a.exec();




#ifndef MYITEM_H_
#define MYITEM_H_
#include <QGraphicsItem>
class MyItem:public QGraphicsRectItem
{
public:
MyItem(QGraphicsItem* parent=0,QGraphicsScene *s=0);
virtual ~MyItem();

QRectF boundingRect() const;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *e);
void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );

};

#endif /*MYITEM_H_*/




#include "MyItem.h"
#include <QtGui>
MyItem::MyItem(QGraphicsItem* p,QGraphicsScene *s):
QGraphicsRectItem(p,s)
{

}

MyItem::~MyItem()
{
}

void MyItem::mousePressEvent(QGraphicsSceneMouseEvent* e){
qDebug("testing");
QGraphicsRectItem::mousePressEvent(e);

}

void MyItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
{
painter->fillRect(10,10,50,50,QBrush(Qt::red,Qt::SolidPatte rn));
}

QRectF MyItem::boundingRect() const
{

}

Wim
25th March 2008, 07:16
You need to make your item selectable. Add the following line to the MyItem constructor:


setFlags(QGraphicsItem::ItemIsSelectable);