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, myQTreeView
with aQFileSystemModel
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.
-
@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:
Then I resize the window:
And even more:
Now the scrollbar appears... but... even if I drag it now to the right completely, it shows:
So to get the scrollbar to appear sooner, I just set the column width to a large value:
ui->treeView->setColumnWidth(0, 1000);
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 tryQHeaderView::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.