Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Triggering action by click ouside a widget



  • I have th following layout
    bf791e2d-9d14-44b8-b3ed-0278d23da3e5-image.png

    When I click on a plot it is displayed with a thicker line to visualize that it is selected. I can then delete it with the delete button.
    For each serie I add to the chart I create a connection to a slot which increases the thickness of the plot.
    connect(serie, &QLineSeries::clicked, this, &AtollMain::handleClickedPlot);

    After a plot has been selected I would like to unselect it when I click anywhere of the Graph or outside the GraphicsView.

    I tried to reimplement the mousePressEvent for the QGraphicsView for unselecting plots, but then when I click on a plot the connection previously setup doesn't work and I cannot select a plot.

    How could I achieve what I'm looking for?


  • Lifetime Qt Champion

    Hi
    When you override the mouse press, you must still call
    the base class mouse press - as else some feature might break.

    If you know if you have something selected, then you could use
    something like

    void QGraphicsView::mousePressEvent(QMouseEvent *e) {
    if (somethingSelected)
      deselect(); // however, it must happen
    else
      QGraphicsView::mousePressEvent(e); // call base class so it can still select stuff
    }
    


  • Thank you very munch.
    As a beginner with Qt I always forget that you have to forward the unhandled events.


Log in to reply