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

Why does my QGraphicsSimpleTextItem not display any plot information when I move my mouse?



  • I have the following function that will display the x and y values of my QChart when my cursor moves:

    void constellationmockup::mouseMoveEvent(QMouseEvent *event)
    {
    m_coordX->setText(QString("X: %1").arg(chart->mapToValue(event->pos()).x()));
    m_coordY->setText(QString("Y: %1").arg(chart->mapToValue(event->pos()).y()));

    }

    where m_coordX and m_coordY are QGraphicsSimpleTextItems. I am able to set their position using this function: m_coordY->setPos("some points")

    The problem is that it will only display the points when my mouse is outside of my plot. When my mouse is hovering inside my plot area, it will not display any information. It pretty much "freezes" until I move my mouse outside of the plot.

    For reference, I got the solution from this function from the "callout" example in QtCreator:

    void View::mouseMoveEvent(QMouseEvent *event)
    {
    coordX->setText(QString("X: %1").arg(m_chart->mapToValue(event->pos()).x()));
    coordY->setText(QString("Y: %1").arg(m_chart->mapToValue(event->pos()).y()));
    QGraphicsView::mouseMoveEvent(event); //I did try inserting this into my function, but I get an error if I include this.
    }

    Any help would be much appreciated. I definitely seem to be missing something!


  • Lifetime Qt Champion

    Hi
    you might need to enable mouse tracking
    http://doc.qt.io/qt-5/qwidget.html#mouseTracking-prop
    not sure if constellationmockup is a QWidget.
    Or you need to set on other object, but its not always
    enabled and it only send moveEvents if button is pressed.



  • @mrjj It is a QWidget. Does that make a difference?


  • Lifetime Qt Champion

    @engsml
    Nope, that's perfect. Normally it wont send mouseMove to QWidgets as far as i recall
    and you need to set mouseTracking to true to have them come without mouse buttons being pressed.



  • @mrjj Yeah, the mouseMoveEvent works on everything but the actual QChart. I can only get the x and y points to display when I left click and drag it into the chart area. Otherwise it won't work. Would I have to include this function: QChartView::mouseMoveEvent(event) for it to work?


  • Lifetime Qt Champion

    @engsml
    hi
    Its only sent to the widget under the mouse cursor.
    So yes, if you want to trap them there, you need to subclass and override that.
    However, if you need mouse info a global scope , maybe look into event filters.
    That allows to capture it across various Widgets without subclassing.
    If you already subclassed QChartView, override is ofcourse most easy.

    http://doc.qt.io/qt-5/eventsandfilters.html



  • @mrjj How would I subclass QChartView? I think that is what I am missing.


  • Lifetime Qt Champion

    hi

    class MyQChartView : public QChartView {
      Q_OBJECT
     public:
      explicit MyQChartView (QWidget* parent = 0) : QChartView(parent) {}
      ~QChartView() {}
    protected:
     virtual void moveEvent(QMoveEvent* event) override {
            .....
     }
    };
    

    If you have the QChartView in UI file, you can use promotion to easy replace it at runtime with your version.
    http://doc.qt.io/qt-5/designer-using-custom-widgets.html
    just give it MyQChartView as class name and MyQChartView .h (if you put it in own file)
    else what ever .h file it lives in.

    update: you might want to call base class version as it might use it.

    virtual void moveEvent(QMoveEvent* event) override {
            QChartView::moveEvent(event); // call base first
    ...
     }
    

  • Lifetime Qt Champion

    updated code. paste error.
    explicit QChartView (QWidget* parent = 0) : QChartView(parent) {}
    --->
    explicit MyQChartView (QWidget* parent = 0) : QChartView(parent) {}



  • @mrjj Thank you! Do I create another .cpp and header file and include this in my project?


  • Lifetime Qt Champion

    @engsml
    Yes, that is the cleanest. (IMHO)
    and then just promote it. ( it sounds complex but really easy after first time)



  • @mrjj Thank you! I will try that



  • @mrjj When I create the new class, what do I make the base class? Also, what does promoting accomplish in the sense that it overrides the QChartView class?


  • Lifetime Qt Champion

    @engsml
    just see code snippet ?
    Like that. its complete override. with mousemove.

    promotion is just easy way to get your type into the UI file.
    Creator just note the actual type you want and at runtime, its your new widget instead.
    Like a runtime replace.
    if you insert the Chart via code, you have no use for promotion.



  • @mrjj Hi. thanks for your help! I ended up not being able to figure it out, but am currently looking into the event filters option. I added QtCharts::QChartView::mouseMoveEvent(event); to my function to pass the event to the chartView but it still won't display any information when the mouse is on the plot. Any ideas? I'm following the "callout" example which has a similar structure.


  • Lifetime Qt Champion

    Hi
    ah, QChartView is basically a QAbstractScrollArea which means it has the

    https://doc.qt.io/qt-5/qabstractscrollarea.html#viewport

    There is a high chance that this viewport objects uses/eats the mouseMove and hence the Widget dont see them
    or viewport have no mouse tracking.

    i might be able to test it tonight.

    Else event filter sounds like a plan. set it on viewport().


Log in to reply