Solved Is it possible to use mouseMoveEvent in TableView to emit signal when mouse hover without clicking?
-
Hello, I'm trying to emit signal when mouse hovering rows in TableView, but I'm getting unexpected behavior when trying to debug it with qDebug(). It seems to be working, but not when mouse cursor just hovering over different rows, but only when clicking and dragging cursor while holding mouse button. Is it even possible to do what I want using mouseMoveEvent? Here's my code:
customview.cpp:#include "customview.h" #include <QMouseEvent> #include <QDebug> CustomView::CustomView(QWidget *parent) : QTableView(parent) { connect(this,SIGNAL(hoverIndexChanged(const QModelIndex&)),parent,SLOT(onHoverIndexChanged(const QModelIndex&))); } void CustomView::mouseMoveEvent(QMouseEvent *event) { QModelIndex index = indexAt(event->pos()); qDebug() << index; emit hoverIndexChanged(index); }
mainwindow.cpp:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { CustomView *view = new CustomView(this); ... } void MainWindow::onHoverIndexChanged(const QModelIndex& index) { int hoverrow = index.row(); //qDebug() << hoverrow; }
-
@Maxxii said in Is it possible to use mouseMoveEvent in TableView to emit signal when mouse hover without clicking?:
void CustomView::mouseMoveEvent(QMouseEvent *event)
You should call the base class here.
-
This post is deleted! -
Hi
Make sure
https://doc.qt.io/qt-5/qwidget.html#mouseTracking-prop
is on.Also are you sure its QTableView itself that gets the mouseMove and not the
viewport ? -
@mrjj Yes, turning mouseTracking on helped indeed. Didn't know about this property. Thank you!