PDA

View Full Version : Custom QGraphicsRectItem cannot receive mouseMoveEvent



Nya
3rd March 2015, 15:20
Item.h:


class Item:public QGraphicsRectItem
{
public:
Item(const QRectF & rect, QGraphicsItem * parent = 0);
void mousePressEvent(QGraphicsSceneMouseEvent * event);
void ​mouseMoveEvent(QGraphicsSceneMouseEvent * event);
void ​mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
~Item();
};

Item.cpp:

Item::Item(const QRectF & rect, QGraphicsItem * parent)
:QGraphicsRectItem(rect, parent)
{
}

void Item::mousePressEvent(QGraphicsSceneMouseEvent * event){
qDebug("press");
QGraphicsRectItem::mousePressEvent(event);
event->accept();
}
void Item::​mouseMoveEvent(QGraphicsSceneMouseEv ent * event){
qDebug("move");
}

Item::~Item(){}
main.cpp:

#include <QApplication>
#include "QGraphicsView"
#include "QGraphicsScene"
#include "item.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// MainWindow w;
// w.show();

auto item = new Item(QRectF(0,0,100,100));

auto scene = new QGraphicsScene;
scene->addItem(item);

auto view = new QGraphicsView(scene);
view->show();

return a.exec();
}

I have already explicitly call accept() after calling the base class implementation in the mousePressEvent(). But it doesn't work