Important: Please read the Qt Code of Conduct -

QFileSystemModel problem with rowCount

  • Dear all,

    I am learning how to use model/view with the Qt documentation

    But I can't make one example working :

    I create a QFileSystemModel, set the root path and want to know the number of rows of an index, here is the main code :

    @QApplication app(argc, argv) ;

    QFileSystemModel* model = new QFileSystemModel ;
    model->setRootPath(QDir::currentPath()) ;

    QModelIndex parentIndex = model->index(QDir::currentPath()) ;
    int rows = model->rowCount(parentIndex) ;
    qDebug() << rows ;

    return app.exec() ;
    rows is always set to 0, no matter the path. I think I'm using the code of the example without any changes so if someone can help me it would be a pleasure.

  • Make sure that your current directory is not empty or change the root path.

  • I have found some explanations on the internet...

    the QFileSystemModel::rowCount() function apparently return a valid result only after app.exec() is called.

    Replacing the QFileSystemModel with a QDirModel, (without using setRootPath() work well.

    That's the way to make the example of "Model/View Programming" working. (QDirModel is obsolete for the rest)

  • If you are learning from the examples ,a thing I have found helpful is to load the examples from your install directory ,run them and figure out how they have achieved what you have already seen working.
    Just find the examples folder in your install.Help examples also have a link to the location of the files of the examples.

    I love the Qt help system.It is just so logical.

Log in to reply