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. QObject::property(const char* name) in QML
QtWS25 Last Chance

QObject::property(const char* name) in QML

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 2 Posters 4.2k Views
  • 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.
  • M Offline
    M Offline
    manuelsch
    wrote on last edited by
    #1

    Hi,

    is it possible (in pure QML or with the help of C++) to define a "fallback getter function" for not-yet-existing-properties of objects?
    Actually, this would correspond to the functionality of QObject::property(...), but that is not called for a QObject accessed from QML, even if it is Q_INVOKABLE.

    Cheers!

    Manuel

    1 Reply Last reply
    0
    • M Offline
      M Offline
      manuelsch
      wrote on last edited by
      #2

      Anyone knows at least if this is possible at all?

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ixSci
        wrote on last edited by
        #3

        Could you please provide some pseudo-code to make your intention more clear?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          manuelsch
          wrote on last edited by
          #4

          Ok, sure:
          @
          myObj.a = 1
          console.log(myObj.a) # this should work fine, because the property "a" was defined before
          console.log(myObj.b) # the property "b" does not exist yet. here a fallback function should be called

          #something like
          myObj.propertyDoesNotExist = function(propertyName) {
          console.log("Property '"+propertyName+"'does not exist, yet")
          return 999;
          }

          and if you access a non-existing property (e.g. "c"), this function should be called

          console.log("c=")
          console.log(myObj.c)

          @
          So the output of the last two lines should be
          @
          Property 'c' does not exist,yet
          c=
          999
          @

          1 Reply Last reply
          0
          • I Offline
            I Offline
            ixSci
            wrote on last edited by
            #5

            I could be wrong but it seems impossible without modification of the Qt core components such as QObject or QtScript module. It seems like you want to have functionality which is not appropriate to JavaScript at all. You can use some convention, like using some unified function for setting and accessing object properties. In such a way you will have full control over the properties.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              manuelsch
              wrote on last edited by
              #6

              Well, actually it would correspond to "QScriptClass::queryProperty":http://doc.qt.nokia.com/latest/qscriptclass.html#queryProperty, but afaik this is not usable in QML, right?

              1 Reply Last reply
              0
              • I Offline
                I Offline
                ixSci
                wrote on last edited by
                #7

                Frankly speaking, I don't know what the QScriptClass is and how it is related to the QML. But QML objects are descendants of the QDeclarativeItem class and it is not related to the QScriptClass . So if my logic is not broken the only way to accomplish your task is to modify QObject to make the setProperty method virtual. But it is a wrong way I believe.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  manuelsch
                  wrote on last edited by
                  #8

                  I'm talking about the ECMA-Script engine in QML, so it is kind of related to QScriptClass.
                  But as long as we don't have access to the QML script engine, it probably is not possible to implement my idea.

                  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