How to set the cursor to pointer when the mouse hovers over QTreeWidgetItem in a QTreeWidget?
-
wrote on 15 Nov 2018, 14:23 last edited by aha_1980
-
Just like below:
I use the qss,
QTreeWidget::item:hover, QTreeWidget::branch:hover { color: rgb(43, 179, 246); cursor: pointer; }
but, qt don't support the property
cursor
.So I want to use
setCursor
, but QTreeWidgetItem is not a widget.....So, someone can give me any advice ?
@Limer
either you can install an eventFilter on the QTreeWidget and catch the mouse move/hover events, and check the index/item at the corresponding position using QTreeWidget::itemAt()/QTreeWidget::indexAt() and call setCursor() and unsetCursor() on leave event.Or you can sublcass QTreeWidget and follow the same pattern for the event filter.
-
@Limer
either you can install an eventFilter on the QTreeWidget and catch the mouse move/hover events, and check the index/item at the corresponding position using QTreeWidget::itemAt()/QTreeWidget::indexAt() and call setCursor() and unsetCursor() on leave event.Or you can sublcass QTreeWidget and follow the same pattern for the event filter.
wrote on 16 Nov 2018, 13:27 last edited by LimerI tried it,
bool MainWindow::eventFilter(QObject* watched, QEvent* event) { if (watched == m_treeWidget) { if (event->type() == QEvent::HoverMove) { QMouseEvent* e = static_cast<QMouseEvent*>(event); // qDebug() << e->globalPos(); if (m_treeWidget->itemAt(m_treeWidget->viewport()->mapFromGlobal(e->globalPos()))) m_treeWidget->setCursor(Qt::PointingHandCursor); else m_treeWidget->setCursor(Qt::ArrowCursor); return true; } } return QMainWindow::eventFilter(watched, event); }
But, it didn't work.
I printed the
pos()
, found that the position wouldn't vary when I moved the mouse. -
I tried it,
bool MainWindow::eventFilter(QObject* watched, QEvent* event) { if (watched == m_treeWidget) { if (event->type() == QEvent::HoverMove) { QMouseEvent* e = static_cast<QMouseEvent*>(event); // qDebug() << e->globalPos(); if (m_treeWidget->itemAt(m_treeWidget->viewport()->mapFromGlobal(e->globalPos()))) m_treeWidget->setCursor(Qt::PointingHandCursor); else m_treeWidget->setCursor(Qt::ArrowCursor); return true; } } return QMainWindow::eventFilter(watched, event); }
But, it didn't work.
I printed the
pos()
, found that the position wouldn't vary when I moved the mouse.@Limer said in How to set the curtor to pointer when the mouse hovers over QTreeWidgetItem in a QTreeWidget?:
But, it didn't work.
more specifically?
Did the event filter get called at all?
Did you catch the HoverMove event?You can also try to install the event filter on the viewport() widget instead.
-
@Limer said in How to set the curtor to pointer when the mouse hovers over QTreeWidgetItem in a QTreeWidget?:
But, it didn't work.
more specifically?
Did the event filter get called at all?
Did you catch the HoverMove event?You can also try to install the event filter on the viewport() widget instead.
wrote on 17 Nov 2018, 08:05 last edited by@raven-worx Maybe because the treewidget's parent is a qmainwindow, and i tried many ways to enable the mousetracking,but failed.
So , i subclass the qtreewidget, redefine the mouseMoveEvent ,and it successed.
Thanks a lot! ~
4/5