Results 1 to 4 of 4

Thread: qdirmodel question: show file number of a folder?

  1. #1
    Join Date
    Sep 2008
    Posts
    23
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default qdirmodel question: show file number of a folder?

    hi, there
    I am using the qdirmodel in treeview to show the dir tree. My question is: how can I show the number of files under the dir besides the name of the dir? Better in one column like "somedir(35)" but one more column is also acceptable. Thanks for help.
    zl2k

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qdirmodel question: show file number of a folder?

    Qt Code:
    1. wrap it into a proxy model or subclass it
    2.  
    3. class ADirModel : public QDirModel
    4. {
    5. public:
    6. ADirModel(...);
    7. QVariant data(const QModelIndex &index, int role) const
    8. {
    9. if (index.isValid() && role == Qt::DisplayRole)
    10. {
    11. if (rowCount(index)>0)
    12. return QString("%1") (%2)").arg(QDirModel::data(index).toString()).arg(rowCount(index);
    13. return QDirModel::data(index);
    14. }
    15. return QDirModel::data(index, role);
    16. }
    17. };
    To copy to clipboard, switch view to plain text mode 
    Untested. But something like that should work. The code assumes that only the first column has children and that this column is the one with the filename that should be extended.
    (You might have to make some adjustments if you want the "(count)" in a different column.)

    Also note that the use of rowCount() for the number of files is not quite correct:
    subdirectories are counted as files, as would "." and ".." be.

    But the example should get you going.

    HTH

  3. #3
    Join Date
    Sep 2008
    Posts
    23
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: qdirmodel question: show file number of a folder?

    Thanks for your suggestion. However, I will only display the path in the treeview, not the file. But I still want to show the true number of files(paths) under the listed dir. I guess the rowCount won't work since the files won't be shown.

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qdirmodel question: show file number of a folder?

    well, you can reimplement hasChildren in your subclass to return false and use the base classes' QDirModel::rowCount to access the number of files present (but not shown).
    Or you simply use QDir::entries() to calculate the number of files independent of rowCount() (probably the best idea).

    HTH

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13

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
  •  
Qt is a trademark of The Qt Company.