A simple indeed treeview does not display anything upon a correct QFileSystemModel



  • It's weird that the code below does not work -- I copied almost everything from the qt4.8 example.
    I can have a window, but nothing is displayed in the treeview. It's completely blank.
    The code below is literally all I have, except main.cpp which I keep as the original.

    What I got from qDebug is 'D:/', so the model is correct.

    BTW, I tried to search for similar issues so that I don't ask silly and duplicated questions, but it seems there is no such engine to poll ...

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        QTreeView *treeView = new QTreeView(this);
        // set up treeview
        QFileSystemModel model;
        model.setRootPath("d:/");
        treeView->setModel(&model);
        qDebug(model.rootPath().toAscii().data());
     
        // Demonstrating look and feel features
        treeView->setAnimated(false);
        treeView->setIndentation(20);
        treeView->setSortingEnabled(true);
        this->setCentralWidget(treeView);
    }
    


  • Hi

    Since you allocate your QFileSystemModel on the stack it's destroyed at the end of MainWindow constructor.

    So you allocate on the heap...

    QFileSystemModel *model = new QFileSystemModel;



  • @Devopia53
    Thank you so much.
    Been playing with C++ for years and now have silly forgotten object life-cycle !
    It is now solved.


Log in to reply
 

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