Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Print warning if referenced QML property is undefined ?
Forum Updated to NodeBB v4.3 + New Features

Print warning if referenced QML property is undefined ?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 1.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    poncho524
    wrote on last edited by aha_1980
    #1

    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.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by fcarney
      #2

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

      Item{
          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.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        @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.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          poncho524
          wrote on last edited by
          #4

          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.

          ick

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved