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. engine.rootContext()->setContextProperty is failing to add property
QtWS25 Last Chance

engine.rootContext()->setContextProperty is failing to add property

Scheduled Pinned Locked Moved Solved QML and Qt Quick
18 Posts 7 Posters 3.1k 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.
  • GrecKoG Offline
    GrecKoG Offline
    GrecKo
    Qt Champions 2018
    wrote on last edited by
    #8

    @jcdelve said in engine.rootContext()->setContextProperty is failing to add property:

    if(engine.rootObjects().isEmpty()) {
    return -1;
    }

    remove that.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jcdelve
      wrote on last edited by
      #9

      Yes, I removed it, and the program runs, but the number I'm looking for doesn't get set to 20 like it should

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #10

        You call init() before your QML is loaded. The fooChanged() signal is fired long before your QML slot (Connections) is ready to receive it.

        Invoke init() after some delay and it will work.

        (Z(:^

        1 Reply Last reply
        1
        • J Offline
          J Offline
          jcdelve
          wrote on last edited by
          #11

          @sierdzio
          Good catch, that would definitely be part of the problem. However, the object is still not recognized in the qml file. Using QT Creator, the object I know should appear highlighted and italicized, but it's not. And when I try and call the .getFoo() function, it says it's not a function

          1 Reply Last reply
          0
          • JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #12

            @jcdelve said in engine.rootContext()->setContextProperty is failing to add property:

            Q_PROPERTY

            public:
                Q_INVOKABLE int getFoo(); // This will work.
            
            1 Reply Last reply
            1
            • J Offline
              J Offline
              jcdelve
              wrote on last edited by jcdelve
              #13

              @JoeCFD That worked! I didn't realize I needed that for every single property.
              I will say it still looks weird, because the object still looks like normal text instead of highlighted or whatever, but it works so I'm not complaining. Thank you!

              JoeCFDJ 1 Reply Last reply
              0
              • J jcdelve

                @JoeCFD That worked! I didn't realize I needed that for every single property.
                I will say it still looks weird, because the object still looks like normal text instead of highlighted or whatever, but it works so I'm not complaining. Thank you!

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #14

                @jcdelve Q_INVOKABLE means the func is visible in qml. I am learning as well.

                1 Reply Last reply
                1
                • sierdzioS Offline
                  sierdzioS Offline
                  sierdzio
                  Moderators
                  wrote on last edited by
                  #15

                  @KroMignon said in engine.rootContext()->setContextProperty is failing to add property:

                  a.text = updateScreen.getFoo() + ""; // to converter int into string

                  You don't need to make getFoo() invokable! Just call the property instead of a function:

                  a.text = updateScreen.foo 
                  

                  (Z(:^

                  1 Reply Last reply
                  0
                  • GrecKoG Offline
                    GrecKoG Offline
                    GrecKo
                    Qt Champions 2018
                    wrote on last edited by
                    #16

                    Do it declaratively like so:

                    // ...
                    Label {
                        text: UpdateScreen.foo
                    }
                    

                    it will automatically update when the fooChanged signal is emitted.
                    Doing Q_INVOKABLE int getFoo(); is ugly.

                    1 Reply Last reply
                    1
                    • J Offline
                      J Offline
                      jcdelve
                      wrote on last edited by
                      #17

                      Interesting. By all other conventions of coding standards, it's bad practice to have a variable directly accessible like that... Is there a reason QT/QML prefers that over doing an invokable getter?

                      sierdzioS 1 Reply Last reply
                      0
                      • J jcdelve

                        Interesting. By all other conventions of coding standards, it's bad practice to have a variable directly accessible like that... Is there a reason QT/QML prefers that over doing an invokable getter?

                        sierdzioS Offline
                        sierdzioS Offline
                        sierdzio
                        Moderators
                        wrote on last edited by
                        #18

                        @jcdelve said in engine.rootContext()->setContextProperty is failing to add property:

                        Interesting. By all other conventions of coding standards, it's bad practice to have a variable directly accessible like that... Is there a reason QT/QML prefers that over doing an invokable getter?

                        Please read up on how Q_PROPERTY system works. You are not accessing a variable, you are accessing a property. QML (via Meta Object System) will use the getter / setter you provide in Q_PROPERTY declaration. It will not access the variable directly.

                        (Z(:^

                        1 Reply Last reply
                        2

                        • Login

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