Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to set the cursor to pointer when the mouse hovers over QTreeWidgetItem in a QTreeWidget?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 6.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • LimerL Offline
    LimerL Offline
    Limer
    wrote on last edited by aha_1980
    #1

    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 ?

    raven-worxR 1 Reply Last reply
    0
    • LimerL Limer

      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 ?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      LimerL 1 Reply Last reply
      3
      • raven-worxR raven-worx

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

        LimerL Offline
        LimerL Offline
        Limer
        wrote on last edited by Limer
        #3

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

        raven-worxR 1 Reply Last reply
        1
        • LimerL Limer

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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

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

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          LimerL 1 Reply Last reply
          2
          • raven-worxR raven-worx

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

            LimerL Offline
            LimerL Offline
            Limer
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved