Search code examples
c++qteventsqt4qgraphicsview

Events in QGraphicsView


I'm having trouble getting events in QGraphicsView working. I've subclassed QGraphicsView and tried to overload the mousePressEvent and wheelEvent. But neither mousePressEvent nor wheelEvent get called.

Here's my code (Edited a few things now):

Declaration:

#include <QGraphicsView>
#include <QGraphicsScene>
class rasImg: public QGraphicsView
{
public:
rasImg(QString file);
~rasImg(void);
    initialize();
QGraphicsView *view;
QGraphicsScene *scene;
private:
virtual void mousePressEvent (QGraphicsSceneMouseEvent  *event);
virtual void wheelEvent ( QGraphicsSceneMouseEvent  * event );
}

Definition:

#include "rasImg.h"
void rasImg::initialize()
{
view = new QGraphicsView();
scene = new QGraphicsScene(QRect(0, 0, MaxRow, MaxCol));
scene->addText("HELLO");
scene->setBackgroundBrush(QColor(100,100,100));
view->setDragMode(QGraphicsView::ScrollHandDrag);
view->setScene(scene);
}
void rasImg::mousePressEvent (QGraphicsSceneMouseEvent  *event)
{
    qDebug()<<"Mouse released";
    scene->setBackgroundBrush(QColor(100,0,0));
}
void rasImg::wheelEvent ( QGraphicsSceneMouseEvent  * event )
{
qDebug()<<"Mouse released";
    scene->setBackgroundBrush(QColor(100,0,0));
}

So, what am I doing wrong?.
Why don't I see a message or background color change when I click the view or use the mouse wheel?


Solution

  • You're not getting the events because they're being handled by the scene object you're creating, not your class.

    Remove the QGraphicsScene *scene; from your rasImg and try something like this for the constructor:

    rasImg::rasImg(QString file)
      : QGraphicsScene(QRect(0, 0, MaxRow, MaxCol))
    {
     addText("HELLO");
     setBackgroundBrush(QColor(100,100,100));
     setDragMode(QGraphicsView::ScrollHandDrag);
    
     view = new QGraphicsView();
     view->setScene(this);
    }
    

    If you want that in two steps, you could do:

    rasImg::rasImg(QString file)
      : QGraphicsScene()
    {
      // any constructor work you need to do
    }
    
    
    rasImg::initialize()
    {
     addText("HELLO");
     setSceneRect(QRect(0, 0, MaxRow, MaxCol));
     setBackgroundBrush(QColor(100,100,100));
     setDragMode(QGraphicsView::ScrollHandDrag);
    
     view = new QGraphicsView();
     view->setScene(this);
    }
    

    The point is that the scene that is displayed must be an actual instance of your rasImg, not an instance of QGraphicsScene.

    If it's the view you're subclassing, do the same thing. The view you're displaying must be an instance of your class, not a plain QGraphicsView.

    rasImg::rasImg(QString file)
        : QGraphicsView()
    {
       // constructor work
    }
    
    void rasImg::initialize()
    {
      scene = new QGraphicsScene(QRect(0, 0, MaxRow, MaxCol));
      scene->addText("HELLO");
      scene->setBackgroundBrush(QColor(100,100,100));
    
      setDragMode(QGraphicsView::ScrollHandDrag);
      setScene(scene);
    }