Solved Default image from QRC resource, but customized image from filesystem?
-
Or, put another way, how to check file existence in QML?
It easy to bind the image source property to a either a qrc: or filesystem path; but how to make it use the qrc: if and only if the file in the filesystem is missing?This is to allow users to customize an image, whose source defaults to a qrc: path unless they upload an alternative file.
I'm aware Googling suggests resorting to C++ for this, but I'm unable to find an example.
-
Hi,
How can your users select the custom image ?
-
QFileInfo can check for the existence of a file. You could have it verify a path and add a command to qml via setContextObject. This would allow you to add utility commands to qml.
-
@SGaist
In my specific case, users upload files externally trough ssh prior to application launch.
I was wondering about an hypotetic File object:Image { File { id: file source: "file:///bitmaps/custom.png" } source: file.exists ? file.source : "qrc:///bitmaps/default.png" }
-
@fcarney
First steps in C++-QML integration. So if I understand correctly:- Create a QObject with two properties, QString source and bool exists
- In code (via signal/slot), when source changes, set exists accordingly via QFileInfo
- Expose the object to QML via setContextObject
I will give it a try, thank you!
Cannot figure out why I was'nt able to find a ready-made code snippet, checking files seems a common need to me.
I'm learning the declarative approach coming from an imperative background: am I doing something wrong or in a non-QML way? -
I would create a slot function called:
bool fileExists(QString path){}
The setContextObject will place functions on the object in the QML namespace. So you will be able to call fileExists from QML.
-
hi,
this seems to work alsoImage { source: "file:///c:/Users/user/Pictures/img.png" onStatusChanged : if(status == Image.Error) source = "qrc:///img.png" }
-
@LeLev
Wow, that is a really nice way to check that. Thanks! -
@LeLev @fcarney
Thank you! This solves my problem!