Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 904
GlassFish Avatar asked Dec 16 '22 17:12

GlassFish


1 Answers

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.

like image 54
GlassFish Avatar answered Jan 09 '23 01:01

GlassFish