Search code examples
c++qtqtreeviewqt4.8

How to know exactly when a user expands a QTreeView item?


Is there a way to know exactly when an expand operation begins when a user, lets say clicks the expand arrow in a QTreeView?

For the case when he double clicks I can catch the double-click event.

I tried reimplementing this slot void expand(const QModelIndex &index); from QTreeView but it doesn't seem to work.

There is a signal called void expanded(const QModelIndex &index); in QTreeView but it seems to be sent after the expansion happened.

I am using QT 4.8.2


Solution

  • This is what I did to get the functionality I needed:

    I reimplemented the mousePressEvent from QTreeView like this

    void MyTreeView::mousePressEvent(QMouseEvent *event)
    {
        QModelIndex clickedIndex = indexAt(event->pos());
        if(clickedIndex.isValid())
        {
            QRect vrect = visualRect(clickedIndex);
            int itemIdentation = vrect.x() - visualRect(rootIndex()).x();
            if(event->pos().x() < itemIdentation)
            {
                if(!isExpanded(clickedIndex))
                {
                 //do stuff
                }
            }
        }
    }
    

    I check to see if the mouse press is left to the text label of the item(meaning on the expand arrow)

    This combined with the double click event gives me what I needed.