Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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?



  • test for undefined

    myImage.source: {
    var path = '../resources/' + somename + '.png'
    if (path !== undefined)
      return path
    else
      return ""
    }
    

    and inline version:

    myImage.source:  '../resources/' + somename + '.png' ?  '../resources/' + somename + '.png' : ""
    


  • @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 :(


  • Qt Champions 2018

    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?


  • Qt Champions 2018

    You don't need to mess with Qt Sources to do that.
    Create a new class subclassing QObject, add and implement a bool fileExists(const QString& fileName) Q_INVOKABLE/slot. Register the class with qmlRegisterSingletonType().

    After that you can do in QML import YourModuleName 1.0 and then YourClass.fileExists(fileName).


  • Qt Champions 2016

    @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 by file://
    checking those QML pathes from C++ you must remove file:// 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.


Log in to reply