Search code examples
qtfunctioncountreturnzero

QT - QDir count() returns zero


I´m new in using QT and I´m having troubles with a QDir count() function.

I have this code

obrazky = new QDir;
obrazky->setPath("obr");
pocet = obrazky->count();

...

textT.append(QString("%1").arg(pocet));
informativeText = new QLabel;
informativeText->setText(textT);

where "obrazky" is declared as QDir *obrazky; I have an "obr" folder in my project folder but the "pocet" variable is still 0. In "obr" folder I have a few image files.

Do you know what I´m doing bad?


Solution

  • You should check that you're actually in your project directory when running this code. A simple call to obrazky->absolutePath() should help here, it will give you the full path name from your relative obr path segment.

    Or, you could equally well use exists() to check that the directory exists (the actual directory, not the one you think it is). Since count() is equivalent to entryList()->count() and entryList() simply returns an empty list if the directory does not exist, that could well be the problem.