PDA

View Full Version : qdirmodel question: show file number of a folder?



zl2k
6th November 2008, 18:12
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

caduel
6th November 2008, 22:10
wrap it into a proxy model or subclass it

class ADirModel : public QDirModel
{
public:
ADirModel(...);
QVariant data(const QModelIndex &index, int role) const
{
if (index.isValid() && role == Qt::DisplayRole)
{
if (rowCount(index)>0)
return QString("%1") (%2)").arg(QDirModel::data(index).toString()).arg(rowCo unt(index);
return QDirModel::data(index);
}
return QDirModel::data(index, role);
}
};
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

zl2k
7th November 2008, 04:00
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.

caduel
7th November 2008, 07:07
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