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. Identifying item from QModelIndex?
Qt 6.11 is out! See what's new in the release blog

Identifying item from QModelIndex?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 720 Views 1 Watching
  • 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

    I have a delegate to my model setup.

    delegate = new MyTreeViewDelegate;
    
    treeView = new MyTreeView;
    treeView->setModel(treeModel);
    treeView->setItemDelegateForColumn(1, delegate);
    

    And here's the paint method.

    void MyTreeViewDelegate::paint(QPainter *painter, 
        const QStyleOptionViewItem &option, const QModelIndex &index) const
    {   
        QString indexData = index.data(Qt::DisplayRole).toString();
    
        if (parent.row() == 0 && (option.state & QStyle::State_MouseOver)) {
            qDebug << "which indexData child item: " << indexData;
        }
    }
    

    I'm trying to identify the item that was hovered over by the mouse. The problem is that ...

    QString indexData = index.data(Qt::DisplayRole).toString();
    

    ... always return "", as if there's nothing in it. But my model does have child items in the QTreeView with text.

    Am I trying to access the data wrong? I'm trying to identify what child item was hovered over with the mouse.

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you also have a custom model ?

      If so, are you sure that it returns something valid ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • S SRaD

        I have a delegate to my model setup.

        delegate = new MyTreeViewDelegate;
        
        treeView = new MyTreeView;
        treeView->setModel(treeModel);
        treeView->setItemDelegateForColumn(1, delegate);
        

        And here's the paint method.

        void MyTreeViewDelegate::paint(QPainter *painter, 
            const QStyleOptionViewItem &option, const QModelIndex &index) const
        {   
            QString indexData = index.data(Qt::DisplayRole).toString();
        
            if (parent.row() == 0 && (option.state & QStyle::State_MouseOver)) {
                qDebug << "which indexData child item: " << indexData;
            }
        }
        

        I'm trying to identify the item that was hovered over by the mouse. The problem is that ...

        QString indexData = index.data(Qt::DisplayRole).toString();
        

        ... always return "", as if there's nothing in it. But my model does have child items in the QTreeView with text.

        Am I trying to access the data wrong? I'm trying to identify what child item was hovered over with the mouse.

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

        @SRaD
        Maybe not to do with your "empty string display value", but I thought that to enable hover tracking you needed to treeView->viewport()->setAttribute( Qt::WA_Hover );?

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

          @JonB
          Yes, I have exactly that line after setItemForDelegateColumn(). I should have posted that as well.

          @Christian-Ehrlicher
          Yes, I have a class that is derived from QAbstractItemModel. When you ask if it returns something valid, what are you referring to there?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @SRaD said in Identifying item from QModelIndex?:

            @Christian-Ehrlicher
            Yes, I have a class that is derived from QAbstractItemModel. When you ask if it returns something valid, what are you referring to there?

            Except I'm not Christian ;-)

            Yes that's what I'm referring to.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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