Reputation: 1345
I want to use an optional argument of type QFileInfo
. What is the canonical way to test if the caller provided a value or if the default was used?
// declaration
void foo(QFileInfo fi = QFileInfo());
// definition
void foo(QFileInfo fi)
{
if ( /* what to test for: "is default" */ ) {
}
}
Usually default-constructed Qt objects have something like isValid()
or isEmpty()
. However, there seems no such thing in QFileInfo
.
One option would be fi.filePath().isEmpty()
. Is there something better / simpler?
Upvotes: 5
Views: 446
Reputation: 126807
The fi.filePath().isEmpty()
seems to be the most straightforward and efficient, given that the effect of calling the default constructor is to create a QFileInfo
with no file reference.
QFileInfo::QFileInfo()
Constructs an empty QFileInfo object.
Note that an empty QFileInfo object contain no file reference.
Upvotes: 3
Reputation: 8239
Additionally you can check if the file exists after you receive the QFileInfo
in your function
void foo(QFileInfo fi)
{
if ( fi.exists() && fi.isFile() ) {
// execute your logic
}
}
Upvotes: 0