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?