QTreeView last column always visible without scrollbars



  • Hey guys. I'm implementing a tree view and I have two columns for each row. The first column can contain a variable length string and the second column displays some short numeric value.

    Basically I am trying to make it so the second column is always visible no matter what and the first column simply takes up the rest of the available space (no scroll bars).

    I was thinking a subclass of QAbstractItemDelegate would be a viable solution if combined with QTreeView::setFirstColumnSpanned. I also considered using an event filter to resize the two columns every time the QTreeView is resized.

    These two potential solutions aside I wanted to see if there were any simpler solutions that I might have overlooked which is why I came here, so any ideas would be much appreciated!

    Kind regards, Aaron.


  • Moderators

    Here's a non-intrusive solution i.e. one that doesn't require modification to the tree or the delegate classes:

    treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
    treeView->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
    
    treeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    auto sb = treeView->horizontalScrollBar();
    connect(sb, &QScrollBar::rangeChanged, [=]{ sb->setValue(sb->maximum()); });
    


  • Thanks very much for the reply. I like that solution! It's compact and does the job well. I am stumped slightly on that last line however. Could you please explain it? It looks like you are connecting the horizontal scroll bars range change signal to a lambda that sets the horizontal scrollbars maximum value, but I can't see why that is required!


  • Lifetime Qt Champion

    Hi,

    That last light makes your tree view scroll to show what's at the new end of the scrollbar.



  • Yeh I thought so however all the columns fit entirely within the width of the view and there is no scroll bar required (hence it is hidden). Without those lines of code however it doesn't work as expected so I am wondering what else setting the scrollbars value is doing in this particular situation!


  • Moderators

    The scrollbar is hidden (because you didn't want it) but it's still there.
    When the widget gets narrow enough the view gets larger than the viewport - normally you would see a scrollbar in this situation but we explicitly hidden it. You can comment out treeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); to see what's going on.
    The default behavior when a scrollbar appears is that the position is set to 0 and maximum value of the scrollbar is adjusted. This would place the first column in viewport and the second one out of it. That's not what we want so we need to scroll to the end to make the second column always visible. A good place to do it is when the maximum value changes, which happens when you resize the widget or the tree content changes), so we connect to that.


Log in to reply
 

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