Important: Please read the Qt Code of Conduct -

Print warning if referenced QML property is undefined ?

  • Is there a way to have the QQmlEngine or QQmlComponant produce warnings if a QML file is referencing a property that doesn't exist?

    For example:

    Item {
      id: item1
      property int myProperty1: 10
    Item {
      id: item2
      property int myProperty2: item1.mispelledMyProperty1

    This will load and not display any issues until you try to print out "myProperty2" and you'll get "undefined". It would be nice if there was a way to get a warning when loading the qml file.

  • @poncho524
    It is valid to have a property set to undefined. You can even set the property explicitly using the "undefined" key word. So to QML it is not an error. If you are concerned that a property is getting set to undefined you could do something like this:

        property int myProperty2: item1.mispelledMyProperty1
        Component.onCompleted: {
            if(myProperty2 == undefined)
                console.log("Danger danger! Will Robinson!")

    Or maybe even call a function that returns a value:

    property int myProperty2: item1.mispelledMyProperty1 == undefined ? func_that_logs_and_returns_value() : item1.mispelledMyProperty1

    Edit: Just realized it was mispelled. So yeah, it should warn about mispelled stuff. Sorry for the rant.

  • @poncho524 said in Print warning if referenced QML property is undefined ?:

    property int myProperty2: item1.mispelledMyProperty1

    My guess is that the reason it does not flag an error is because of lazy evaluation. It doesn't do anything with it until its used. Where your C++ compiler checks everything even if it is never used. I ran into this same issue with Python. You don't see errors until it tries to run the code. Not sure there is a good fix.

  • I suppose if you really wanted to get crazy you could crawl thru the metadata of each QQuickItem and check for property values of "unknown". I'm thinking you'd only want something like this available during development/debug.


Log in to reply