Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView last column always visible without scrollbars
Forum Updated to NodeBB v4.3 + New Features

QTreeView last column always visible without scrollbars

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aatwo
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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()); });
      
      1 Reply Last reply
      2
      • A Offline
        A Offline
        aatwo
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aatwo
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by Chris Kawa
              #6

              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.

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved