Search code examples
linuxqthome-directoryqfilesystemmodel

Can't show anything above home directory in QFileSystemModel in Linux


I have the following code in Qt4.8 under Linux:

 QFileSystemModel* dir_model = new QFileSystemModel;
 dir_model->setRootPath("/usr");
 ui->dir_tree->setModel(dir_model);

Instead of showing "/usr" the tree widget shows my home directory. If I provide anything above my home (like "/", "/etc", "/media") it always show my home and nothing above. If I provide something inside my home like "/home/semen/something" it shows exactly this directory with no problems. I can't figure out what I'm doing wrong. What should I do to show for example "/media/something"?


Solution

  • you then have to set root index for the view.

    QFileSystemModel* dir_model = new QFileSystemModel;
    QModelIndex rootIndex = dir_model->setRootPath("/usr");
    ui->dir_tree->setModel(dir_model);
    ui->dir_tree->setRootIndex(rootIndex);  // you are missing this step