How to set the cursor to pointer when the mouse hovers over QTreeWidgetItem in a QTreeWidget?



  • Just like below:

    0_1542291536181_8.gif

    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 ?


  • Moderators

    @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.



  • @raven-worx

    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.


  • Moderators

    @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.



  • @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! ~


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.