Search code examples
qtqt4

Check if directory is empty


I'm trying to check if a directory is empty.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QDir Dir("/home/highlander/Desktop/dir");
    if(Dir.count() == 0)
    {
        QMessageBox::information(this,"Directory is empty","Empty!!!");
    }
}

Whats the right way to check it, excluding . and ..?


Solution

  • Well, I got the way to do it :)

    if(QDir("/home/highlander/Desktop/dir").entryInfoList(QDir::NoDotAndDotDot|QDir::AllEntries).count() == 0)
    {
        QMessageBox::information(this,"Directory is empty","Empty!!!");
    }