4

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

GlassFish
  • 14,851
  • 3
  • 17
  • 22
  • What is it that you're trying to do between the time the user tries to expand it and before it actually gets expanded. – Chris Oct 29 '12 at 14:54
  • @Chris I want to change the item label from ITEMNAME to ITEMNAME(Loading) – GlassFish Oct 29 '12 at 14:57
  • You should be able to do that just fine if you have a slot hooked up to the expanded signal. Your slot will be executed before the GUI gets redrawn. – Chris Oct 29 '12 at 15:26
  • What difference it makes if you set "Loading" text after it was expanded? End result will be same. Expanded index and "Loading" suffix – Kamil Klimek Oct 29 '12 at 15:38
  • @KamilKlimek it's not my call :( if it was I couldn't care less – GlassFish Oct 29 '12 at 15:48
  • @Chris I'll try it and see if it works, but between the "User clicking to expand" and "TreeItem expanding" there is a considerable wait time. – GlassFish Oct 29 '12 at 15:51
  • @Dragarro Maybe you should point out to "caller" that there is no big difference in this behaviour? – Kamil Klimek Oct 29 '12 at 16:16
  • This is still a problem in Qt 5.15. Calling isExpanded(index) in QTreeView shows the correct bool when read in mouseEvent but when the expand/collapse button is pressed, the bool is already updated in mouseEvent, making manual expand/collapse impossible. – misantroop Dec 10 '20 at 07:59

2 Answers2

12

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.

GlassFish
  • 14,851
  • 3
  • 17
  • 22
  • There is still no better solution 8 years later. Having collapsible index items and trying to expand/collapse on index click and the expand/collapse indicator does not work together with any other solution than yours. The collapse/expand slot override in QTreeView does not work as mentioned. – misantroop Dec 10 '20 at 09:42
0

You could try to this by implementing a method for a change event:

  void YourTreeView::changeEvent(QEvent *e)
  {
    QFrame::changeEvent(e);

    switch (e->type()) {
    case QEvent::QGraphicsSceneResizeEvent
        //do want you want to do
        break;
    default:
        break;
    }
  }
TWE
  • 431
  • 2
  • 11