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. Why is there no horizontal scrollbar in a QTreeView with QFileSystemModel?
Forum Update on Monday, May 27th 2025

Why is there no horizontal scrollbar in a QTreeView with QFileSystemModel?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 6.1k Views
  • 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.
  • DiracsbracketD Offline
    DiracsbracketD Offline
    Diracsbracket
    wrote on last edited by Diracsbracket
    #1

    Hi,
    I have tried several versions of Qt now, and in one of those, my QTreeView with a QFileSystemModel as model has a working horizontal scrollbar.

    Even if I set

    m_dirTree->horizontalScrollBar()->setEnabled(true);
    m_dirTree->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    

    , only an empty disabled horizontal scrollbar shows.

    Why is that? It is highly annoying to have to manually resize the treeview horizontally every time I want to have a look/access to folders somewhat deeper down the hierarchy.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Try adding: m_dirTree->header()->setStretchLastSection(true);

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      DiracsbracketD 1 Reply Last reply
      1
      • VRoninV VRonin

        Try adding: m_dirTree->header()->setStretchLastSection(true);

        DiracsbracketD Offline
        DiracsbracketD Offline
        Diracsbracket
        wrote on last edited by Diracsbracket
        #3

        @VRonin said in Why is there no horizontal scrollbar in a QTreeView with QFileSystemModel?:

        Try adding: m_dirTree->header()->setStretchLastSection(true);

        Thanks, @VRonin!

        I must add that in the model, I only keep the actual tree structure, i.e. I hide the all columns except column 0 (I don't want the date, etc...).

        In a minimal test program, I added a QTreeView to the main form, and then set the following in the constructor:

        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            m_treeModel = new QFileSystemModel(ui->treeView);
            m_treeModel->setFilter(QDir::AllDirs | QDir::Drives | QDir::NoDotAndDotDot | QDir::Files);
            m_treeModel->setNameFilters({"*.txt"});
            m_treeModel->setNameFilterDisables(false);
        
            ui->treeView->horizontalScrollBar()->setEnabled(true);
            ui->treeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
        
            ui->treeView->setModel(m_treeModel);
            ui->treeView->setRootIndex(m_treeModel->setRootPath(QString()));
        
            //hide the columns we don't want
            for (int i=1; i<4; ++i) ui->treeView->hideColumn(i);
        
            ui->treeView->header()->setStretchLastSection(true);
        }
        

        This shows the following:

        0_1518541347806_efb6c227-3a22-42ee-8768-45e921af8d1f-image.png

        Then I resize the window:

        0_1518541419810_54b7a8da-2ddb-4f8d-87ea-ab23bdd521d6-image.png

        And even more:

        0_1518541447236_ad38680b-cfe2-49da-9861-ad5a3d279b46-image.png

        Now the scrollbar appears... but... even if I drag it now to the right completely, it shows:

        0_1518541512826_b01beb42-d5e7-4e26-8fe7-7c96578de057-image.png

        So to get the scrollbar to appear sooner, I just set the column width to a large value:

        ui->treeView->setColumnWidth(0, 1000);
        

        0_1518542416480_266b3540-70c8-4e4e-b9d7-b39705778c81-image.png

        So it works, kind of. Now there is large white gap at the end, because the width is so large.

        I'll need to look if there is way to intercept a signal when a node is expanded and connect that signal to a slot that adjusts the width of column 0?...

        1 Reply Last reply
        0
        • DiracsbracketD Offline
          DiracsbracketD Offline
          Diracsbracket
          wrote on last edited by Diracsbracket
          #4

          The following seems to work:

          connect(ui->treeView, SIGNAL(expanded(QModelIndex)), this, SLOT(onExpanded(QModelIndex)));
          connect(ui->treeView, SIGNAL(collapsed(QModelIndex)), this, SLOT(onExpanded(QModelIndex)));
          

          and then

          void MainWindow::onExpanded(const QModelIndex& index)
          {
              ui->treeView->resizeColumnToContents(0);
          }
          

          So I don't need to set the arbitrary big column width!

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Sorry, yes, so instead of stretching the last section you have to stretch the first ui->treeView->header()->setSectionResizeMode(0,QHeaderView::Stretch); if it doesn't work try QHeaderView::ResizeToContents instead

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            DiracsbracketD 1 Reply Last reply
            3
            • VRoninV VRonin

              Sorry, yes, so instead of stretching the last section you have to stretch the first ui->treeView->header()->setSectionResizeMode(0,QHeaderView::Stretch); if it doesn't work try QHeaderView::ResizeToContents instead

              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on last edited by Diracsbracket
              #6

              @VRonin said in Why is there no horizontal scrollbar in a QTreeView with QFileSystemModel?:

              ui->treeView->header()->setSectionResizeMode(0,QHeaderView::Stretch); if it doesn't work try QHeaderView::ResizeToContents instead

              Edit:
              None of these seem to work: when resizing the widget, the scrollbar does not appear as expected.
              What I am doing now is just add:

              m_dirTree->resizeColumnToContents(0);
              

              in the onSelectionChanged() handler of the tree, which requires me to click on the item (and change the selection) to make it adjust its width and spawn the scrollbar. No need for the above two signal connects then either.

              1 Reply Last reply
              1

              • Login

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