Qt mouseMoveEvent does not work with QColumnView

1 week ago 10
ARTICLE AD BOX

I have a QColumnView with a QFileSystemModel attached to it, and I am trying to get information on what file is being hovered over in that model view.

I do have mouse tracking enabled, however it seems that mouseMoveEvent only reports anything when an empty column is being hovered over.

Oddly enough, my exact same code, but with the QColumnView replaced with a QTreeView works fine. I cannot imagine this is a bug given the widgets library is very stable, but I do not see anything on the QColumnView, QAbstractItemView, QMouseEvent or QWidget docs or online about this difference in behavior or how to remedy it, given the QColumnView is a relatively unused view. I also tried overriding the entire QColumnView::event method, but have not had any luck with that either.

Here is a minimum reproducible example:

MainWindow.cpp

#include "mainwindow.hpp" #include "./ui_mainwindow.h" #include "mycolumnview.hpp" #include <QFileSystemModel> #include <QLayout> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { qDebug() << "Test"; ui->setupUi(this); MyColumnView *columnView = new MyColumnView(this); this->layout()->addWidget(columnView); } MainWindow::~MainWindow() { delete ui; }

MainWindow.hpp

#ifndef MAINWINDOW_HPP #define MAINWINDOW_HPP #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_HPP

mycolumnview.cpp

#include "mycolumnview.hpp" #include <QDebug> MyColumnView::MyColumnView(QWidget *parent) : QColumnView(parent) { // Enable mouse tracking to receive events even if no button is pressed setMouseTracking(true); setMinimumWidth(500); setMinimumHeight(500); model->setRootPath(QDir::homePath()); setRootIndex(model->index(QDir::homePath())); setModel(model); } void MyColumnView::mouseMoveEvent(QMouseEvent *event) { QColumnView::mouseMoveEvent(event); QModelIndex index = indexAt(event->pos()); if (index.isValid()) { QString fileName = index.data(Qt::DisplayRole).toString(); QString filePath = index.data(Qt::EditRole).toString(); // EditRole often contains the full path for QFileSystemModel qDebug() << "Mouse moved over file:" << fileName << " at path:" << filePath; } else { qDebug() << "Mouse moved over empty space."; } }

mycolumnview.hpp

#ifndef MYCOLUMNVIEW_H #define MYCOLUMNVIEW_H #include <QColumnView> #include <QMouseEvent> #include <QModelIndex> #include <QFileSystemModel> class MyColumnView : public QColumnView { Q_OBJECT public: MyColumnView(QWidget *parent = nullptr); protected: void mouseMoveEvent(QMouseEvent *event) override; QAbstractItemView* createColumn(const QModelIndex& index) override; private: QFileSystemModel* model = new QFileSystemModel; }; #endif // MYCOLUMNVIEW_H

Edit: I have also added the following file and modification to try and get this to work

eventfilter.hpp

#ifndef EVENTFILTER_HPP #define EVENTFILTER_HPP #include <QObject> #include <QEvent> #include <QDebug> #include <QMouseEvent> class MyEventFilter : public QObject { Q_OBJECT public: explicit MyEventFilter(QObject *parent = nullptr) : QObject(parent) {} protected: bool eventFilter(QObject *obj, QEvent *event) override { if (event->type() == QEvent::MouseMove) { QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); qDebug() << "Mouse moved from event filter at:" << mouseEvent->pos(); return false; // Allow the target object to also receive the event } // For other event types, pass them on to the base class return QObject::eventFilter(obj, event); } }; #endif // EVENTFILTER_HPP

and I added this to mycolumnview.cpp:

QAbstractItemView* MyColumnView::createColumn(const QModelIndex& index) { QAbstractItemView* view = QColumnView::createColumn(index); view->setMouseTracking(true); // Try enabling mouse tracking on each individual item MyEventFilter* filter = new MyEventFilter(view); view->installEventFilter(filter); return view; }
Read Entire Article