Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    3
    5
    2143
    Loading More Posts
    • 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
      SRaD last edited by

      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.

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @SRaD last edited by JonB

        @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 Reply Quote 3
        • S
          SRaD last edited by

          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;
          
              // ...
          }
          
          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @SRaD last edited by mrjj

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

            1 Reply Last reply Reply Quote 3
            • S
              SRaD last edited by

              @mrjj Thanks, that fixed it.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post