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

Set path of setRootIndex using a directory of project



  • Hi everyone
    I would like to set the index of a QTreeView with the path of a directory that is included in my project written in C ++.
    So, using a folder on my desktop as path, I have:

    model = new QFileSystemModel();
    model->setRootPath("C:/Users/Giusi/Desktop/Utils");
    directory = new QTreeView(widget);
    directory->setModel(model);
    directory->setRootIndex(model->index("C:/Users/Giusi/Desktop/Utils"));
    

    And everything is fine.
    The problem arises when I want to use a folder present in my project because it no longer stops doing Build, as if it went in a loop.
    To include the folder, I use Qt Resource System.
    I also use this mechanism for some icons and everything works perfectly when I use QPixmap like this

    QPixmap pixmapWheelX (":/qres/icons/wheel.png");
    

    Any suggestions?


  • Lifetime Qt Champion

    Hi
    Im not sure when issue comes ?

    Do you do something like
    directory->setRootIndex(model->index(":/qres/icons/"));

    Or what you mean ?



  • @mrjj sorry, I think I didn't explain myself well.
    My goal is set setRootIndex with the path of a folder which is included in my project.
    So, I included the folder using Qt Resource System, and when I use it in
    model->index("path")
    and I build the project, compilation does not end.

    QPixmap pixmapWheelX (":/qres/icons/wheel.png") was an example to explain another point where I use Qt Resource System -and this works.


  • Lifetime Qt Champion

    @giusirux
    Hi
    So when you say "included in my project.", you mean its added to a resource file that is compiled into the project ?

    and if you then use this resource file for the model, it will then compile for ever ??



  • @mrjj said in Set path of setRootIndex using a directory of project:

    So when you say "included in my project.", you mean its added to a resource file that is compiled into the project ?
    and if you then use this resource file for the model, it will then compile for ever ??

    yes, that's right


  • Lifetime Qt Champion

    @giusirux

    Hi
    I tried to reproduce this by
    1: add a sub folder to the project folder
    2: add this folder to a resource file (included in the project)
    3: use QFileSystemModel with this sub folder
    however, nothing bad happened.
    (it just compiled)

    SO did I miss a step ?

    What Qt version are you using ?



  • @mrjj Hi
    The steps are right
    My version is: Qt Creator 4.12.4
    Based on Qt 5.14.2 (MSVC 2017, 32 bit)


  • Lifetime Qt Champion

    @giusirux Not the version which QtCreator was build with, we need the version of Qt you're compiling your apps with.



  • @Christian-Ehrlicher yes, sorry
    Qt 5.13.2



  • ok, the Build problem is solved, but the path is not correct when the application runs.
    The folder, which I included in the project, has three subfolders.
    So, after I added the folder to a resource file, I copied and pasted the path in setRootPath and setRootIndex, but the folder which I see when the application runs is "C:"
    Any suggestions?


  • Lifetime Qt Champion

    Hi,

    AFAIK, qrc files shall contain a list of file paths. Putting a folder in there will not recursively add the files and subfolders in there.



  • @SGaist hi
    oh, ok... I didn't know it. So, how can i get this TreeView including the folder "Utils" in the project?
    e2e0c23d-1a1c-4f29-97f0-6c82f9a728fc-image.png

    P.S. these are Utils' subfolders


Log in to reply