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 call a function by hovering over a TreeView's item?
QtWS25 Last Chance

How to call a function by hovering over a TreeView's item?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 3.2k 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.
  • S Offline
    S Offline
    SRaD
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • S SRaD

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @SRaD
      You do this at the view side, QTreeView and the QAbstractItemView 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

      1 Reply Last reply
      3
      • S Offline
        S Offline
        SRaD
        wrote on last edited by
        #3

        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;
        
            // ...
        }
        
        mrjjM 1 Reply Last reply
        0
        • S SRaD

          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;
          
              // ...
          }
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          Can you try with
          myTreeView->viewport()->setAttribute( Qt::WA_Hover );

          1 Reply Last reply
          3
          • S Offline
            S Offline
            SRaD
            wrote on last edited by
            #5

            @mrjj Thanks, that fixed it.

            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