Question

I am starting up in QT for a simple drawing application and i have problem in with the mouse event handles of QGraphicsView.

Here is a summary of the code (please bear in mind this is made to get the basic operation right only. The main just creates a QMainWindow-like element that has the following constructor:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    myCanvas = new GraphicsView;
    myCanvas -> sizeGS(0, 0, 800, 600);
    setCentralWidget(myCanvas -> getView());
    resize(800,600);
}

my Canvas belongs to the class derived from QGraphicsView where any mouse press event is not registered at all.

class GraphicsView : public QGraphicsView
{
    Q_OBJECT
public:
    explicit GraphicsView(QObject *parent = 0);
    ~GraphicsView(){};
    void sizeGS(int x0, int y0, int x1, int y1);
    QGraphicsView* getView() {return canvasView;};

protected:
    operationType currentOperation;

private:
    static const int exMaxPolygonSize = 10;

    QGraphicsScene *canvasScene;
    QGraphicsView *canvasView;

    std::vector<QPointF> mousePos;
    QList<QGraphicsItem *> draftedItems;
    QPointF topLeft;
    QPointF bottomRight;

    void resetLineDrawing();
    void completeDrawing();
    void mousePressEvent(QMouseEvent *mouseEvent);
};

and the key parts of the class are the following two functions:

GraphicsView::GraphicsView(QObject *parent) :
    QGraphicsView()
{
canvasScene = new QGraphicsScene;
canvasView = new QGraphicsView(canvasScene);
mousePos.reserve(exMaxPolygonSize);
//canvasView->setMouseTracking(true);
canvasView->setRenderHints( QPainter::Antialiasing );

qDebug() << "constructor is ok";
}

void GraphicsView::mousePressEvent(QMouseEvent *event)
{
qDebug() << "mouse grabbed!";
};

The mousePressEvent have been tried also with test on the buttons and so on like with no difference.

Any help to understand what is that i am missing would help. I am using QT5.2 on OSX.

Thanks

EDIT: This code is for under standing how to control the mouse events for a QGView being central widget of a QMainWindow.

Was it helpful?

Solution

Your GraphicsView class inherits QGraphicsView, which is not used in your code. Try the following : in the main window constructor, use your GraphicsView instance directly as a QGraphicsView :

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    myCanvas = new GraphicsView;
    //myCanvas -> sizeGS(0, 0, 800, 600);
    setCentralWidget(myCanvas);
    resize(800,600);
}

In GraphicsView header, do not embed another QGraphicsView ! (I comment useless lines out)

class GraphicsView : public QGraphicsView
{
    Q_OBJECT
public:
    explicit GraphicsView(QObject *parent = 0);
    ~GraphicsView(){};
    //void sizeGS(int x0, int y0, int x1, int y1);
    //QGraphicsView* getView() {return canvasView;};

protected:
    operationType currentOperation;

private:
    static const int exMaxPolygonSize = 10;

    QGraphicsScene *canvasScene;
    //QGraphicsView *canvasView;

    std::vector<QPointF> mousePos;
    QList<QGraphicsItem *> draftedItems;
    QPointF topLeft;
    QPointF bottomRight;

    void resetLineDrawing();
    void completeDrawing();
    void mousePressEvent(QMouseEvent *mouseEvent);
};

Finally, in your GraphicsView cpp file, just use it as expected :

GraphicsView::GraphicsView(QObject *parent) :
    QGraphicsView()
{
canvasScene = new QGraphicsScene;
//canvasView = new QGraphicsView(canvasScene); 
this->setScene(canvasScene); // here you associate the scene with the view
setSceneRect(0, 0, 800, 600); // override your sizeGS() call

mousePos.reserve(exMaxPolygonSize);
//canvasView->setMouseTracking(true);
canvasView->setRenderHints( QPainter::Antialiasing );

qDebug() << "constructor is ok";
}

void GraphicsView::mousePressEvent(QMouseEvent *event)
{
qDebug() << "mouse grabbed!";
};

Now you should see mousePressEvents, because you actually use the intended GraphicsView

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top