Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. changing QML variables from c++
Forum Updated to NodeBB v4.3 + New Features

changing QML variables from c++

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 1.6k 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.
  • S Offline
    S Offline
    shokarta
    wrote on 6 Jul 2020, 12:32 last edited by
    #1

    hello,

    as I have in QML File:

    property bool dbOnline
    onDbOnlineChanged: { console.log("variable changed"); }
    

    and then somehow from C++ i wish to change this variable so the change will be noticed by QML and will debug the "variable changed"

    can you show an example how to do that please?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 6 Jul 2020, 13:00 last edited by
      #2

      Hi
      Did you try
      https://doc.qt.io/qt-5/qqmlproperty.html

      https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shokarta
        wrote on 6 Jul 2020, 13:05 last edited by
        #3

        @mrjj hello,
        yes I have checked those, however they are object properties,
        i dont know how to implement this fo common QML variable :(

        M 1 Reply Last reply 6 Jul 2020, 14:11
        0
        • S shokarta
          6 Jul 2020, 13:05

          @mrjj hello,
          yes I have checked those, however they are object properties,
          i dont know how to implement this fo common QML variable :(

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 6 Jul 2020, 14:11 last edited by
          #4

          @shokarta
          Im not sure you can as all sample i have seen is been via what shown in those links/docs.
          But Im not super QML expert so give it some days and see if you get
          a solution.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shokarta
            wrote on 8 Jul 2020, 13:57 last edited by
            #5

            BUMP,
            noone know if this is realy possible?
            Why should not be realy...

            K J 2 Replies Last reply 8 Jul 2020, 14:21
            0
            • S shokarta
              8 Jul 2020, 13:57

              BUMP,
              noone know if this is realy possible?
              Why should not be realy...

              K Offline
              K Offline
              KroMignon
              wrote on 8 Jul 2020, 14:21 last edited by
              #6

              @shokarta said in changing QML variables from c++:

              noone know if this is realy possible?

              May be possible, but I don't see use case for this.
              The "Qt way" would be to have a C++ QObject which hold the "shared" variable and which will be exposed to QML.
              Doing this from QML side is not a good practice in my eyes.

              Please read this https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html.
              This could help you to do things in the best way.
              And never forget this fundamental rule: if you fighting against the Qt/QML framework, then you are probably doing things in the wrong way.

              juste my 2 cts

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              3
              • S shokarta
                8 Jul 2020, 13:57

                BUMP,
                noone know if this is realy possible?
                Why should not be realy...

                J Offline
                J Offline
                Jarek B
                wrote on 8 Jul 2020, 16:19 last edited by Jarek B 7 Sept 2020, 11:49
                #7

                @shokarta
                Basically what you are trying to accomplish is having variable on the QML side that will be a state of something else (that is probably kept on the C++ side). You are introducing another variable that needs to be synchronized, and its only purpose is to cache something that should be available on the C++ side.

                Imagine you have QObject of class DBConnector that is exposed to a QML and this object have bool property called online. Now on the QML side you can simply do

                    Connections {
                        target: App.Context.dbConnector
                        onOnlineChanged: {
                            console.log("variable changed")
                        }
                    }
                

                App.Context is in this case just a simple class with bunch of properties/objects that are accessible in the QML by:

                import Application 1.0 as App
                

                It can be registered using qmlRegisterSingletonType on the C++ side
                https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType-1
                For Qt < 5.14 you have to use static method or free function instead of lambda.

                You can also use QQmlContext::setContextPropery but it is encouraged to use much safer alternatives.

                EDIT:
                I have no knowledge about QQmlContext::setContextPropery been marked as deprecated (I oversimplified some news I have heard). Only thing I know for sure is that there are much safer ways of achieving the same results that will be encouraged in the current and next releases.

                O 1 Reply Last reply 8 Jul 2020, 16:55
                3
                • J Jarek B
                  8 Jul 2020, 16:19

                  @shokarta
                  Basically what you are trying to accomplish is having variable on the QML side that will be a state of something else (that is probably kept on the C++ side). You are introducing another variable that needs to be synchronized, and its only purpose is to cache something that should be available on the C++ side.

                  Imagine you have QObject of class DBConnector that is exposed to a QML and this object have bool property called online. Now on the QML side you can simply do

                      Connections {
                          target: App.Context.dbConnector
                          onOnlineChanged: {
                              console.log("variable changed")
                          }
                      }
                  

                  App.Context is in this case just a simple class with bunch of properties/objects that are accessible in the QML by:

                  import Application 1.0 as App
                  

                  It can be registered using qmlRegisterSingletonType on the C++ side
                  https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType-1
                  For Qt < 5.14 you have to use static method or free function instead of lambda.

                  You can also use QQmlContext::setContextPropery but it is encouraged to use much safer alternatives.

                  EDIT:
                  I have no knowledge about QQmlContext::setContextPropery been marked as deprecated (I oversimplified some news I have heard). Only thing I know for sure is that there are much safer ways of achieving the same results that will be encouraged in the current and next releases.

                  O Offline
                  O Offline
                  ODБOï
                  wrote on 8 Jul 2020, 16:55 last edited by ODБOï 7 Aug 2020, 16:55
                  #8

                  hi
                  @Jarek-B said in changing QML variables from c++:

                  Connections {
                  target: App.Context.dbConnector
                  onOnlineChanged: {
                  console.log("variable changed")
                  }
                  }

                  if you use Qt 5.15 Connections syntax has changed

                   Connections {
                           target: App.Context.dbConnector
                           function onOnlineChanged() {
                               console.log("variable changed")
                           }
                       }
                  
                  1 Reply Last reply
                  2

                  1/8

                  6 Jul 2020, 12:32

                  • Login

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