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



  • 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.



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



  • @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?...



  • 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!



  • 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



  • @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.


Log in to reply
 

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