QHeaderView mouse tracking fails
-
I need to connect some simple filter functionality, to a mouse click on a QTreeView header item. Simple enough, I implemented a slot function that connects to:
QTreeView::header()->sectionClicked(int)
After setting
QTreeView::header()->setSectionsClickable(true)
,sectionClicked is emitted any time I click on a header that is highlighted by the default hover effect that any clickable header will produce.
The issue is, hovering over some areas in clickable headers will not be recognized. Hence, in these parts there is no highlight and I will not get any sectionClicked triggers. I traced it back further and derived my own class from QHeaderView and put some output into mouseMoveEvent.
class MyHeaderView : public QHeaderView { Q_OBJECT public: MyHeaderView(QWidget* parent = 0) : QHeaderView(Qt::Horizontal, parent) { setMouseTracking(true); } protected: virtual void mouseMoveEvent(QMouseEvent* event) { qDebug() << event->pos(); } };
While leaving all QTreeView settings as they were, I set an instance of this class to be the header via
QTreeView::setHeader(QHeaderView*)
I could see, that in all areas were the hover effect failed, I did not get the debug output you can see in the overridden mouseMoveEvent.
As a result I am assuming, that the mouse tracking is not working correctly.
The overall application I am working on is quiet big, so I set up a stand alone example, for all of this. To my surprise, everything works as expected. I am clueless, how I should proceed with this. Can anyone think of reasons for the mouse tracking to fail in some parts of a widget? Could it be a frame rate issue tied to lack of performance? Are there settings on widgets that affect the overall mouse tracked area? Can parent widgets affect mouse tracking?
-
Hi and welcome to devnet,
Do you have other subclass of QHeaderView that are used in your application ?
-
@SGaist Sorry I am only replying now. I thought I'd get an email if there was a reply to my question. Maybe there was and it just landed in the spam folder. I have been able to solve it.
I have a CategoryWidget in my application, which contains a CategoryTreeView and a CategoryController instance. The CategoryTreeView is derived from QTreeView. CategoryController connects to a bunch of signals emitted from the tree view and handles application logic connected to these. It should not have any visual representation. Hence, the best choice for a base class would be QObject. Shamefully, I have after a while noticed that I had it set up as a QWidget.
Since there is should not be a visualization for the controller, I obviously did not bother adding it to a layout in the CategoryWidget. As a result, it overlayed the tree view with an invisible widget area, which disallowed MouseEvents on the QTreeView underneath it.After changing the CategoryControllers BC to QObject everything works as expected.
I guess this bug was a pretty good general lesson, to be more careful with BC definitions.
Thanks!
-
You're welcome !
You can also check in your profile settings whether the email notification is active.
Since you have it working now, please mark the thread as solved using the "Topic Tool" button so that other forum users may know a solution has been found :)