I am trying to implement in my program, that if the mouse hovers over my QChartView widget, I get back the coordinates of the cursor.
I already tried, by installing an event filter on the widget:
ui->chartView->setMouseTracking(true);
ui->chartView->installEventFilter(this);
and then writing the method for a mouse event:
void MainWindow::mouseMoveEvent(QMouseEvent* event) {
qDebug() << event->pos();
}
But, I only get the output when I click on the Mainwindow and hold the mouse button, which I clicked. When I click on the widget chartView
, I don't get any output.
I need to get output, when the mouse is hovering over chartview
.
Installing an event filter means you want to customize event handling inside QObject::eventFilter, so reimplementing
mouseMoveEvent
goes against the point of using an event filter.So you do not get output because you have not reimplemented the
eventFilter
, andmouseMoveEvent
does not get triggered unless you do setMouseTracking(true);.Supposing that
this
isMainWindow
, here is an example reimplementation ofeventfilter
:Note: if you install your
mainWindow
as an event filter for multiple objects, you need to check the object as well before customizing the event handling, to avoid undesired behavior.Example:
Or