How do I determine whether a file exists?
-
Specifically, I want to specify the source file for a QML Image by something like
myImage.source = '../resources/' + somename + '.png'
This works just fine as long as the specified image file exists.
How can I validate that it exists and take some appropriate action if it does not?
-
-
@Eligijus said in How do I determine whether a file exists?:
test for undefined
Thanks. Unfortunately, I've tried that and it doesn't work.
In the example, path is defined, but because the statement "path = " defines it.
What I need to determine is whether the file (as defined by path) exists or not.
But maybe I've missed or mis-understood something?
-
@Eligijus said in How do I determine whether a file exists?:
test for undefined
Thanks. Unfortunately, I've tried that and it doesn't work.
In the example, path is defined, but because the statement "path = " defines it.
What I need to determine is whether the file (as defined by path) exists or not.
But maybe I've missed or mis-understood something?
@drmhkelley You are correct, I confused it with something different. And from the looks of it you can't do that check from qml side. You can check from c++ side with QFile::exists() method http://doc.qt.io/qt-5/qfile.html#exists
Also if you are playing with relative urls, Qt.resolvedUrl() could come in handy for you http://doc.qt.io/qt-5/qml-qtqml-qt.html#resolvedUrl-method . -
Thanks. Qt.resolvedUrl() was one of the things I had tried, with same results as your initial suggestion.
I feared that QML wouldn't suffice and that I'd need to retreat to Qt C++ directly. Sadly, this isn't the first time I've bumped into that.
I've now made a couple of forays into QML because it seems so simple and because some relevant Qt provided examples were built in QML. However, thus far EVERY project that I've started in QML ultimately required a full port into C++ because necessary functionality just wasn't there.
I'll cut my losses and write this one off a just another of my failures to learn from prior mistakes :(
-
You can expose c++ helpers to extend QML functionnality.
-
Yes, that is possible.
But as far as I know, such an approach to my present issue would require messing was the source for the underlying Qt libs themselves - for the current and all future releases. Not sure that’s a path I want to follow too far.
Am I missing something?
-
You don't need to mess with Qt Sources to do that.
Create a new class subclassingQObject
, add and implement abool fileExists(const QString& fileName)
Q_INVOKABLE
/slot. Register the class withqmlRegisterSingletonType()
.After that you can do in QML
import YourModuleName 1.0
and thenYourClass.fileExists(fileName)
. -
@GrecKo already told you how easy it is to check if file exists from QML invoking a C++ method.
be aware if your file is located anywhere at the file system, there's a small difference between QML and C++ file pathes:
QML file pathes are prefixed byfile://
checking those QML pathes from C++ you must removefile://
before checking if exists -
Thanks. I seem to be acquiring quite a library of such QML extensions to cover what seem to be significant deficiencies of an otherwise pretty impressive programming platform. However, it's still not completely clear that QML has been a net gain in capability. So far, I'm writing that off to my own inexperience, but it remains an issue of active contemplation.