Kousalik
Kousalik

Reputation: 3157

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.

Upvotes: 5

Views: 6253

Answers (3)

Juampa
Juampa

Reputation: 41

Due to my low reputation I cannot make comments, then here I put some lines about QFileInfo:

QFileInfo fileInfo("./.py");
bool test = fileInfo.exists() && fileInfo.isFile();

that example will fail!

I added in the check the baseName() value:

bool test = fileInfo.exists() && fileInfo.isFile() && !fileInfo.baseName().isEmpty();

Upvotes: 0

Prem Kumar
Prem Kumar

Reputation: 9

You can directly use

QFileInfo fileInfo(path)
bool test = fileInfo.exists() && fileInfo.isFile()

Upvotes: 0

asclepix
asclepix

Reputation: 8061

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());

Upvotes: 9

Related Questions