Important: Please read the Qt Code of Conduct -

QGraphicsScene mouse x, y question

  • in Qt Designer i added a QGraphicsView to mainwindow. I did not add a separate class for Graphics.
    I want to get mouse x,y position relative to QGraphicView during hover and when left-clicked.
    Not the math approach by substracting x and y from widget geometry.
    I want to learn how to get events from a widget. In Signal /Slot Editor, i can not find a
    Signal from QGraphicView ( mouseclicked) or similar.

    Ui::MainWindow *ui;

    QGraphicsScene          *scene_1;
    QGraphicsEllipseItem    *ellipse;
    QGraphicsRectItem       *rectangle;
    QGraphicsTextItem       *text;

    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    @// constructor:
    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    scene_1 = new QGraphicsScene(this);

    } @
    @void MainWindow::mousePressEvent(QMouseEvent *event)

    if (event->button() == Qt::LeftButton) {
        qDebug() << "press_evt";
       //  relative to mainwindow
       qDebug() << event->pos().x() << event->pos().y();
       // relative to Widget ??
       qDebug() << ???   x,y position of mouse when over QGraphicView-Widget no mousebutton active
       qDebug() << ???   x,y position of mouse when mouse-left-click, maybe as QPoint 


    The above is all i figured out so far. I know its not a lot.
    The mouse press event is triggered in mainwindow area and QGraphicView area and
    i assume i get x,y mouse position relative to mainwindow in this mousePressEvent function.
    thank you

  • How about subclassing QGraphicsView, and getting the mouse coordinates from the mouse event methods there?

    Edit: Oh, I see. A bit tricky inside a UI File.

    There's still the possibility to add an event filter, though. See QObject::installEventFilter

  • ok, i'll do so, thank you !
    (i assume no quick & easy solution here)

  • I tried your suggestion and i "subclassed" QGraphicsScene (at least i think so) and it works. I always have problems to create the correct class header, especially the top part line[6-10] in header:
    @#ifndef MYGRAPHICS_H
    #define MYGRAPHICS_H

    #include <QGraphicsScene>

    class MyGraphics : public QGraphicsScene
    explicit MyGraphics(QObject *parent = 0);

    // overriding mouse events
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);

    @void MyGraphics::mousePressEvent(QGraphicsSceneMouseEvent *event)
    qDebug() << "pressed";
    qDebug() << event->scenepos().x();

    " // overriding mouse events"
    -what are the consequences of "overriding mouse events" ?

    -what is the difference between inheriting and subclassing ?

    btw.: I fear i have big deficit in understanding the fundamentals of C++.
    i'll fix this, i promise :)

  • Inheriting and Subclassing (in this context) mean pretty much the same thing.

    Overriding or reimplementing a method means that the method of your derived class will be called instead of the base class method.

    However, you should always consider whether you really want to replace the base class behavior, or whether you just want to add to it.
    In the second case, make sure you explicitly call the base class method as well. In your code, you should add
    otherwise the events will not be correctly handled anymore.

  • thank you !

Log in to reply