PDA

View Full Version : How to intercept mouse events in a scene



markmuetz
22nd May 2008, 14:32
I've subclassed the QGraphicsScene class into my own class, and tried to re-implement the method
QGraphicsScene::mousePressEvent(QGraphicsSceneMous eEvent *event)
so as I can do something interesting when the use clicks on the view (namely cause a QGraphicsItem object to appear). On running the code and clicking in the view though, this method is not called.

Here are the relevant parts of code (as I see them), can anybody tell me what I'm missing?

DiagramScene *scene = new DiagramScene();
graphicsView = new QGraphicsView(scene);



class DiagramScene : public QGraphicsScene
{
Q_OBJECT

public:
DiagramScene(QObject *parent = 0);

protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent);
};



DiagramScene::DiagramScene(QObject *parent)
: QGraphicsScene(parent)
{
}

void DiagramScene::mousePressEvent(QGraphicsSceneMouseE vent *mouseEvent)
{
if (mouseEvent->button() != Qt::LeftButton)
return;
//do something here...
}

wysota
22nd May 2008, 14:41
Use a qDebug() statement to see if your event handler gets called - it should. It might not behave as you expect it, but it should at least be called.

markmuetz
22nd May 2008, 15:36
The problem was that I was re-setting the scene in a different place, so as a new standard QGraphicsScene was being used. qDebug() statements are useful (easier to use than gdb :), thanks.