[Solved] QFileSystemModel root path



  • It says in the documentation that QFileSystemModel::setRootPath does not set the root path that is visible in the tree view. Is there a way to set the real root path, so that no directories above it are visible?



  • Hi,

    well, you're right, the documentation is a bit confusing. In the class description, it says:

    QFileSystemModel will not fetch any files or directories until setRootPath is called. This will prevent any unnecessary querying on the file system until that point such as listing the drives on Windows.

    ... but later, in setRootPath, it reports what you say.

    I didn't verify this by myself ... in any case, if it's true that the model cannot hide directories that are above a specific point, you should use a QSortFilterProxyModel attached to it, and filter out the directories you don't need (i.e., those that don't have the root path equal to the one you specify).

    T.



  • You mean, given /a/b, /a/c and /a/d, you want to set "/a/b/" as root path, and the model should show an item for "b" but not for "a" and "c"? I don't believe this is possible out of the box, you would have to use a QSortFilterProxyModel indeed (and reimplement filterAcceptsRow).



  • Hi, I want to do same thing, I guess.

    I want to give the root folder to the program, and the tree should show the root and the sub folders.

    @QTreeView *treeView = new QTreeView;
    QFileSystemModel *sourceModel = new QFileSystemModel();
    QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel();

    proxyModel->setSourceModel(sourceModel);
    *proxyModel->setFilterRegExp(QRegExp("C:", Qt::CaseInsensitive,QRegExp::FixedString));
    proxyModel->setFilterKeyColumn(0);
    treeView->setModel(proxyModel);

    treeView->resize(640, 480);
    treeView->show();@

    I did like this, but it returns me nothing.
    How can I fix this?



  • As stated above and in the docs:
    [quote]QFileSystemModel will not fetch any files or directories until setRootPath is called. This will prevent any unnecessary querying on the file system until that point such as listing the drives on Windows.[/quote]

    As for starting off at a certain node, would setRootIndex do the trick? You can retreive the index to use as root by using QFileSystemModels methods to get a QModelIndex for a path.


Log in to reply
 

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