Search code examples
c++qtqt4qgraphicsview

QGraphicsView subclass and events


In my project I have a QGraphicsView and a QGraphicsScene. I need to add some extra events to my view so I've subclassed QGraphicsView.

The problem is that when I set a mousePressEvent I override the drag mode ScrollHandDrag.

My question is the following: is there a way to switch between the default QGraphicsView answer to mousePressEvent and a custom one (using the m_click to differentiate the different cases for instance)?

Here is my code:

MyQGraphicsView.h

class MyQGraphicsView : public QGraphicsView
{
    Q_OBJECT
    public:
    MyQGraphicsView(QGraphicsScene *scene, QWidget *parent = 0);

    public slots:
    // void mousePressEvent(QMouseEvent * e);

    private:
    QGraphicsScene *m_scene;
    int m_click;
};

MyQGraphicsView.cpp

MyQGraphicsView::MyQGraphicsView(QGraphicsScene *scene, QWidget *parent) :
    QGraphicsView(parent),
    m_scene(scene),
    m_click(0)
{
    setScene(m_scene);
}

/*void MyQGraphicsView::mousePressEvent(QMouseEvent * e)
{
    double rad = 1;
    QPointF pt = mapToScene(e->pos());
    if (m_click)
    {
        m_scene->addEllipse(pt.x()-rad, pt.y()-rad, rad*2.0, rad*2.0, QPen(), QBrush(Qt::SolidPattern));
        m_click = 0;
    }
    else
    {
        m_click = 1;
    }
}*/

Basically what I would like the code to do is: when m_click=1, draw a point, but when m_click=0 use the ScrollHandDrag.

I can make both work separately but not at the same time.


Solution

  • if ( !m_click ) {
        QGraphicsView::mousePressEvent(e);
    }
    

    Just call the parent class' implementation to use the 'normal' behaviour.