Search code examples
c++qtfileqfile

QFileInfo exists() and isFile() error


I'm trying to check if provided path exists and if it is a file.
So I wrote this piece of code:

#include <QFile>
#include <QFileInfo>


bool Tool::checkPath(const QString &path){
    QFileInfo fileInfo(QFile(path));
    return (fileInfo.exists() && fileInfo.isFile());
}

I get following compiler errors:

Error: request for member 'exists' in 'fileInfo', which is of non-class type 'QFileInfo(QFile)'

Error: request for member 'isFile' in 'fileInfo', which is of non-class type 'QFileInfo(QFile)'

Why ? I'm reading through docs over and over again but I can't get it. BTW Qt Creator is suggesting me these methods and completes them. But compiler don't like it.


Solution

  • It seems vexing parse: compiler thinks that

    QFileInfo fileInfo(QFile(path));
    

    is a function definition like QFileInfo fileInfo(QFile path);

    Use instead something like:

    QFile ff(file);
    QFileInfo fileInfo(ff);
    bool test = (fileInfo.exists() && fileInfo.isFile());