Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED]QFileSystemModel set root path



  • hi,
    I'm trying to set the root path to a specific directory , but it doesn't work .
    this is my code :
    @
    QString fPath = "APP_sessions/";
    files = new QFileSystemModel(this);
    QStringList filters;

    filters << "*.C";
    filters << "*.sqlite";
    
    files->setNameFilters(filters);
    files->setNameFilterDisables(false);
    if(QDir(fPath).exists()){
        QWidget tmp;
        QMessageBox::information(&tmp,"exist","exist");
    }
    files->setRootPath(fPath);
    

    @
    the message box (for testing purposes) pops out which means that qt recognizes the directory , but the treeview starts with the c: drive and lists only the drives on the machine (c: d: ...).
    Im on windows Qt .


  • Lifetime Qt Champion

    Hi,

    You should rather give the full path to setRootPath. I haven't check but setting a relative path to QFileSystemModel doesn't sound like the right thing to do.



  • this is the full path :
    @
    QString fPath = "C:\Users\saeedh\QT_projects\home_work\build-ACS_tool-Desktop_Qt_5_3_MSVC2013_32bit-Debug\APP_sessions";
    @
    its without spaces and doen't work , this time it doesn't even pop the message which means it doesn't recognize the directory .



  • Hi,
    maybe following link helps:
    "Link":http://qt-project.org/forums/viewthread/1846



  • [quote author="Vovasty" date="1413279412"]Hi,
    maybe following link helps:
    "Link":http://qt-project.org/forums/viewthread/1846[/quote]
    sorry but my problem is treeview doesn't even set my directory , i dont care if other directories also displayed .


  • Lifetime Qt Champion

    It's an invalid path. A backslash in a strings is an escape character. Either escape your backslashes (use \ in your string) or since you are using Qt, use the unix notation: forward slashes, less error prone.



  • [quote author="SGaist" date="1413279766"]It's an invalid path. A backslash in a strings is an escape character. Either escape your backslashes (use \ in your string) or since you are using Qt, use the unix notation: forward slashes, less error prone.[/quote]

    it helped with recognizing the directory (the message now pops up) , but not with the root tree view path (back as original post problem)



  • QFileSystemModel* fsModel = new QFileSystemModel(this);
    fsModel->setRootPath("c:/file qt/");
    fsModel->setReadOnly(true);
    fsModel->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot);
    QStringList filter;
    filter<<"*.txt";
    fsModel->setNameFilters(filter);
    


  • QFileSystemModel* fsModel = new QFileSystemModel(this).
    fsModel->setRootPath("c:/file qt/");
    fsModel->setReadOnly(true);
    fsModel->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot);
    QStringList filter;
    filter<<"*.txt";
    fsModel->setNameFilters(filter);


  • Lifetime Qt Champion

    You need to also call setRootIndex in your tree view. You have the details in the documentation from QFileSystemModel



  • *@@



  • @QFileSystemModel* fsModel = new QFileSystemModel(this);
    fsModel->setRootPath("c:/file qt/");
    fsModel->setReadOnly(true);
    fsModel->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot);
    QStringList filter;
    filter<<"*.txt";
    fsModel->setNameFilters(filter);@


  • Lifetime Qt Champion

    No need to post your code again, you can simply edit your answer to add the coding tags



  • [quote author="SGaist" date="1413280688"]You need to also call setRootIndex in your tree view. You have the details in the documentation from QFileSystemModel[/quote]

    now it works , thanks a lot , all i added was :
    @
    ui->treeView->setRootIndex(files->index(fPath));
    @
    after you set the tree view model



  • I'm sorry . are new .


  • Lifetime Qt Champion

    You're welcome !

    No problem, everybody falls for that one ;)

    Happy coding !



  • If you are using treeview can also try qDirModel
    @ QStringList filters;
    filters << "*.txt";
    model = new QDirModel(this);
    model->setReadOnly(false);
    model->setReadOnly(true);
    model->setSorting(QDir::DirsFirst |QDir::IgnoreCase | QDir::Type);
    model->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot);
    model->setNameFilters(filters);

    ui->treeViewDirectory->setModel(model);
    QModelIndex index=model->index("C:/file qt/");
    ui->treeViewDirectory->expand(index);
    ui->treeViewDirectory->scrollTo(index);@

  • Lifetime Qt Champion

    QDirModel has been obsoleted, QFileSystemModel is the right class to use



  • ok ok


Log in to reply