Re: QTreeView appears un-ordered and without a scrollbar on startup
Hello,
I have a problem making QTreeView work as expected.
I'm trying to make a simple filesystem tree using QFileSystemModel. I made this to work, but when I start my application, in the first 0.5 - 1 seconds of running the program the tree appears un-ordered (beginning with the home folder) and without a scrollbar. Example pictures are as follows:
This is as it appears on the cold startup for the first 0.5 - 1 seconds:
http://andromeda.kiwilight.com/~dule...e_unsorted.png
This is as it appears after the previous image, that is, after 0.5 - 1 seconds:
http://andromeda.kiwilight.com/~dule...ple_sorted.png
My code is as follows:
mainwindow.cpp
Code:
#include <QtGui>
#include "mainwindow.h"
MainWindow::MainWindow() {
// create system models ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treeModel = new QFileSystemModel(this);
treeModel
->setFilter
(QDir::NoDotAndDotDot |
QDir::AllDirs);
treeModel->setRootPath("/");
// tree ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treeDock->setTitleBarWidget(dummyWidget);
treeView->setModel(treeModel);
treeView->setHeaderHidden(true); //no headers
treeView->setUniformRowHeights(false); //better performance
treeView->hideColumn(1);
treeView->hideColumn(2);
treeView->hideColumn(3);
treeView->hideColumn(4);
treeView->setRootIndex(treeModel->index("/"));
treeView->setCurrentIndex(treeModel->index(startPath));
treeView->scrollTo(treeModel->index(startPath));
treeDock->setWidget(treeView); //add tree to dock
this->addDockWidget(Qt::LeftDockWidgetArea, treeDock);
// statusbar ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
status = statusBar();
}
(I do need the dock)
mainwindow.h
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
{
Q_OBJECT
public:
MainWindow();
private:
QFileSystemModel *treeModel;
};
#endif // MAINWINDOW_H
main.cpp
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
=> My QTCreator project file: download here
*
=> I forgot to mention the problem (maybe I wasn't clear), I'm trying to eliminate that interval when the tree is un-ordered and without a scrollbar. I want it to appear like in the second picture from the start.
I was going nuts trying to solve this but no luck...
Any help would be greatly appreciated!
Re: QTreeView appears un-ordered and without a scrollbar on startup
QFileSystemModel fills itself on demand using a worker thread. Thanks to that it doesn't block the application while directories are being scanned. If you want to eliminate this behaviour then either use QDirModel instead of QFileSystemModel or wait until the model is populated before showing it in the tree.
Re: QTreeView appears un-ordered and without a scrollbar on startup
Quote:
Originally Posted by
wysota
QFileSystemModel fills itself on demand using a worker thread. Thanks to that it doesn't block the application while directories are being scanned. If you want to eliminate this behaviour then either use
QDirModel instead of
QFileSystemModel or
wait until the model is populated before showing it in the tree.
This (the bolded part) is what I want to do, but I have no idea what code to use.
Re: QTreeView appears un-ordered and without a scrollbar on startup
Don't set the model on the tree until it finishes populating.
Re: QTreeView appears un-ordered and without a scrollbar on startup
Qt Documentation says QDirModel is obsolete.
Quote:
Don't set the model on the tree until it finishes populating.
directoryLoaded() signal can be used, set the view model in the slot connected to this signal
Re: QTreeView appears un-ordered and without a scrollbar on startup
I use a work-around for the described (a little ugly) behaviour.
Derive a class MyTreeView from QTreeView and add the following:
mytreeview.h:
Code:
Q_SLOT
void onDirectoryLoaded(const QString&);
mytreeview.cpp:
Code:
void YFileTree::onDirectoryLoaded(const QString&)
{
Model->sort(0); // the column for sorting
}
MyTreeView::MyTreeView()
{
// ...
connect(Model,SIGNAL(directoryLoaded(const QString&)),
this,SLOT(onDirectoryLoaded(const QString&)));
// ...
}