Highlight QtreeView row on mouse hover
-
Hi all,
I have a cpp qwidget with a QTreeview and a QSqlTableModel inside. I would like to highlight on mouse hover an entire row. Curenntly the behaviour is made to highlight just one cell. Do you have any suggestion how to do this.
What I tried:
- Set the stylesheet
ui->myTableView->setStyleSheet("::item:hover{background-color: rgb(249, 175, 42);}");
- Sublcass the QSqlTableModel in order to overwrite the data method using the Qt::BackgroundRole
// setData() method if (role == Qt::BackgroundRole) { QBrush redBackground(Qt::red); return redBackground; }
and on the view I installed the event filter in order to catch the mouse hover event and then then call the setData() method. My problem is that this gets overwritten by the current stylesheet of the application, and when I disable the stylesheet then the setData set the brush for all the cells. How can I manage this?
Thanks.
Simone -
Use a custom QStyledItemDelegate and set QStyle::State_MouseOver in QStyleOptionViewItem.state for every index in your row or derive from QTableView,overwrite initViewItemOption() and set State_MouseOver there (only for Qt6).
Note: some styles might ignore this flag.