Results 1 to 10 of 10

Thread: listing derectory contents

  1. #1
    Join Date
    Apr 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default listing derectory contents

    Hi,

    I am new to Qt prgramming but have fair amount of knowledge to work with Qt. I have requirement to list the directry structure of different projects folders like

    + projects
    |
    + Qt project
    |
    + c:\Qt ## actual directory listing starts here
    |
    ....
    + Qtopia prject
    |
    + d:\Qtopia ## actual directory listing starts here
    |
    ....

    I know i can use the QDirModel, but that doesnt give the required.

    here
    1. these derectories are from different drives
    2. the first item should always be "projects"
    3. the next immediate items will be names of the projects

    can anybody help me? expecting piece of code also

    thanks in advance
    ram

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: listing derectory contents

    You can build your own model (or a proxy model) that will return different results depending on the depth - The top most item ("projects") can be hardcoded, the next level would operate on some list of projects and below that you could return data from a proper QDirModel.

  3. #3
    Join Date
    Apr 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: listing derectory contents

    thanks wysota,

    yes i thought the same and implemented the first part as to get projects and name of the projects from the list. now the problem is how to integrate this with QDirModel. How can i use the QDirModel for the required path?

    if you can elaborate the answer/ piece of code wuld be great

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: listing derectory contents

    Have a QDirModel member, map indexes and call the model's implementation of appropriate methods.
    Pseudocode follows:
    Qt Code:
    1. QVariant myModel::data(...){
    2. if( depth(index) >=2){
    3. QModelIndex ind = mapToDirModel(index);
    4. return dirModel->data(ind, role);
    5. } else {
    6. if(!index.parent().isValid()){
    7. if(role==Qt::DisplayRole) return tr("Projects"); else {...}
    8. } else { ... }
    9. }
    10. return QVariant();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Probably the hardest part is to make a proper implementation of QAbstractItemModel::parent().

  5. #5
    Join Date
    Apr 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: listing derectory contents

    i cant think of implementing this from beginning. it seems to be beyond my level as am getting into this. i request you to give complete code for similar application if you have any.
    thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: listing derectory contents

    Quote Originally Posted by ramamurthy.kv View Post
    i request you to give complete code for similar application if you have any.
    If I had and I could give it to you, I already would have.

    It's not that hard to implement what you want using the dir model, you should really try it. You only have to come up with a nice way to bind "virtual" and "real" paths. It is possible that it is enough to store a file path of the root of the project within the list storing projects.

  7. #7
    Join Date
    Apr 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: listing derectory contents

    Hi,

    could you varify my approch and give answers for my questions?

    i am using the concept of SimpleTreeModel(example program) to list the top level structure i, e,
    + projects
    + Qt Project
    + d:\qt
    + Qtopia
    + c:\qtopia
    now, from "d:\qt" or "c:\qtopia" i would like to use the QDirModel to list the directory structure.
    is this valid aproach?
    if yes, how to integrate QDirModel with the SimpleTreeModel?

    is it possible to use the two model? how?

    any low level design details would be great

    Thanks

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: listing derectory contents

    The approach looks fine. As for combining two models - you'll have to "delegate" access to one model to the other. So for instance if someone asks your model for data for an index that describes a file in a project, you'll have to translate the index to an index understood by the dir model, call the dir model's method with this crafted index and return the result as a result of your method. A simple sketch of what I mean (look in my earlier post for an extended version):
    Qt Code:
    1. QVariant MyModel::data(const QModelIndex &index, ...) const {
    2. QModelIndex dirIndex = translateToDirModel(index);
    3. return internalDirModel->data(dirIndex, ...);
    4. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Apr 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: listing derectory contents

    Thanks Wysota,

    I am finding difficult in "mapping the index (of my model) to index of dir model" and also finding the depth of an index. Somehow i managed to code for finding the depth of an index. I have attached the source code. Please verify the depth function and some more hints on mapping the index(i would request the complete code).

    As I said earliar, i am using the simpletreemodel code. currently am hardcoded the values in setupModelData.

    Thanks
    Attached Files Attached Files

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: listing derectory contents

    Finding depth is easy. Just iterate to parent() index and count how many iterations it takes until you reach the top.

    Qt Code:
    1. QModelIndex iter = index;
    2. int depth = 0;
    3. while(!iter.isNull()){
    4. iter = iter.parent();
    5. ++depth;
    6. }
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.