Identifying item from QModelIndex?



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


  • Lifetime Qt Champion

    Hi,

    Do you also have a custom model ?

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



  • @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 );?



  • @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?


  • Lifetime Qt Champion

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


Log in to reply
 

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