Results 1 to 6 of 6

Thread: QTreeView appears un-ordered and without a scrollbar on startup

  1. #1
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy 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:



    This is as it appears after the previous image, that is, after 0.5 - 1 seconds:



    My code is as follows:

    mainwindow.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "mainwindow.h"
    4.  
    5. MainWindow::MainWindow() {
    6.  
    7. QString startPath = QDir::currentPath();
    8.  
    9. // create system models ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    10.  
    11. treeModel = new QFileSystemModel(this);
    12. treeModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs);
    13. treeModel->setRootPath("/");
    14.  
    15. // tree ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    16.  
    17. treeDock = new QDockWidget("Tree view", this);
    18. treeView = new QTreeView(treeDock);
    19.  
    20. QWidget* dummyWidget = new QWidget(); //remove dock titlebar
    21. treeDock->setTitleBarWidget(dummyWidget);
    22.  
    23. treeView->setModel(treeModel);
    24. treeView->setHeaderHidden(true); //no headers
    25. treeView->setUniformRowHeights(false); //better performance
    26. treeView->hideColumn(1);
    27. treeView->hideColumn(2);
    28. treeView->hideColumn(3);
    29. treeView->hideColumn(4);
    30.  
    31. treeView->setRootIndex(treeModel->index("/"));
    32. treeView->setCurrentIndex(treeModel->index(startPath));
    33. treeView->scrollTo(treeModel->index(startPath));
    34.  
    35. treeDock->setWidget(treeView); //add tree to dock
    36. this->addDockWidget(Qt::LeftDockWidgetArea, treeDock);
    37.  
    38. // statusbar ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    39. status = statusBar();
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 

    (I do need the dock)

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtCore>
    6. #include <QtGui>
    7.  
    8. class MainWindow : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MainWindow();
    14.  
    15. private:
    16. QFileSystemModel *treeModel;
    17. QTreeView *treeView;
    18. QDockWidget *treeDock;
    19. QStatusBar *status;
    20. QMenuBar *menuBar;
    21.  
    22. };
    23.  
    24. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    => 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!
    Last edited by karabaja4; 12th May 2011 at 12:41.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView appears un-ordered and without a scrollbar on startup

    Quote Originally Posted by wysota View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeView appears un-ordered and without a scrollbar on startup

    Don't set the model on the tree until it finishes populating.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTreeView appears un-ordered and without a scrollbar on startup

    Qt Documentation says QDirModel is obsolete.

    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

  6. #6
    Join Date
    May 2014
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. Q_SLOT
    2. void onDirectoryLoaded(const QString&);
    To copy to clipboard, switch view to plain text mode 

    mytreeview.cpp:
    Qt Code:
    1. void YFileTree::onDirectoryLoaded(const QString&)
    2. {
    3. Model->sort(0); // the column for sorting
    4. }
    5.  
    6. MyTreeView::MyTreeView()
    7. {
    8. // ...
    9. connect(Model,SIGNAL(directoryLoaded(const QString&)),
    10. this,SLOT(onDirectoryLoaded(const QString&)));
    11. // ...
    12. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1.  appears in QTextBrowser
    By szisziszilvi in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2011, 08:21
  2. QTreeView and unwanted scrollbar
    By theprobe in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2011, 08:19
  3. QPlastiqueStyle and QTreeView Scrollbar
    By yazwas in forum Qt Programming
    Replies: 0
    Last Post: 13th November 2009, 23:03
  4. Replies: 4
    Last Post: 16th October 2009, 09:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.