How to call a function by hovering over a TreeView's item?
-
Using Qt5, I've got a QTreeView and a model that inherits from QAbstractItemModel similar to the following:
`- Root |- System1 | |- subsystem1 | `- subsystem2 `- System2 |- subsystem3 `- subsystem4
What I'm looking to do is call a function as the mouse hovers over one of the subsystem item's in the TreeView's row and even change the color of the row's text. I need to do this without clicking on the item, just a mouse-over hover.
Any help much appreciated.
-
@SRaD
You do this at the view side,QTreeView
and theQAbstractItemView
it inherits from. There are a couple of approaches, e.g. if all you decide you want to do is change color or whether you do want a function to be called. Take a look at a couple of examples:
https://stackoverflow.com/questions/43035378/qtreeview-item-hover-selected-background-color-based-on-current-color
https://stackoverflow.com/questions/28802763/highlight-item-with-mouse-hover-in-qtreeview
https://stackoverflow.com/questions/53452304/qtreeview-how-to-call-an-action-when-mouse-hovers-over-a-row -
I'm trying to capture when the mouse hovers over a specific row and column in my TreeView. I've added a class which inherits from QStyledItemDelegate and set it to a column, and overridden the paint(...) method. The paint method gets called, but QStyle::State_MouseOver does not seem to be capturing the event.
Thoughts where I am going wrong on this?
mainWindow.cpp
tvd = new MyTreeViewDelegate; treeView = new MyTreeView; treeView->setModel(treeModel); treeView->setItemDelegateForColumn(0, tvd);
treeViewDelegate.cpp
void MyTreeViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QModelIndex parent = index.parent(); if (parent.row() == 0 && (option.state & QStyle::State_MouseOver)) std::cout << "we have mouseover!" << std::endl; // ... }