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. How can I get the application QmlEngine ?
QtWS25 Last Chance

How can I get the application QmlEngine ?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
38 Posts 4 Posters 8.4k 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.
  • J J.Hilk
    30 Nov 2020, 15:31

    @kmarconi said in How can I get the application QmlEngine ?:

    Yeah I already been there but every string which needs to be translated are already marked as qsTr so ...

    so, you answered your own question

    Refreshes all binding expressions that use strings marked for translation. What is that "marked" thing and how to apply it ? Thanks

    😉

    K Offline
    K Offline
    kmarconi
    wrote on 30 Nov 2020, 15:33 last edited by
    #14

    @J-Hilk Yeah but I thought that maybe there was another thing to do ... I'm stuck now so I'm trying everything I can Haha

    1 Reply Last reply
    0
    • K kmarconi
      30 Nov 2020, 15:29

      @J-Hilk Yeah I already been there but every string which needs to be translated are already marked as qsTr so ...

      K Offline
      K Offline
      KroMignon
      wrote on 30 Nov 2020, 15:38 last edited by
      #15

      @kmarconi said in How can I get the application QmlEngine ?:

      Yeah I already been there but every string which needs to be translated are already marked as qsTr so ...

      My way to ensure translation is the define a global Object which handle language changes.
      This object is exposed to QML with engine->rootContext()->setContextProperty("translation", &myLocalesHandler).

      This object has a property called emptyString:

          Q_PROPERTY(QString emptyString     READ emptyString     NOTIFY languageChanged)
      ....
          QString emptyString() const
          {
              return QString();
          }
      

      On language change signal languageChanged() is emitted and in QML I have something like this:

      Label {
          text: qsTr("This is a message") + translation.emptyString
      }
      

      Works great for me, hope this could also helps you.

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

      K 1 Reply Last reply 1 Dec 2020, 08:18
      2
      • F Offline
        F Offline
        fcarney
        wrote on 30 Nov 2020, 15:39 last edited by
        #16

        When I have objects that live in qml space such as a plain QObject, I will make it based upon QQuickItem in order to get the qml engine. I often do this for image providers that provide QImages to qml. I think any QQuick based object should be able to do this.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • K KroMignon
          30 Nov 2020, 15:38

          @kmarconi said in How can I get the application QmlEngine ?:

          Yeah I already been there but every string which needs to be translated are already marked as qsTr so ...

          My way to ensure translation is the define a global Object which handle language changes.
          This object is exposed to QML with engine->rootContext()->setContextProperty("translation", &myLocalesHandler).

          This object has a property called emptyString:

              Q_PROPERTY(QString emptyString     READ emptyString     NOTIFY languageChanged)
          ....
              QString emptyString() const
              {
                  return QString();
              }
          

          On language change signal languageChanged() is emitted and in QML I have something like this:

          Label {
              text: qsTr("This is a message") + translation.emptyString
          }
          

          Works great for me, hope this could also helps you.

          K Offline
          K Offline
          kmarconi
          wrote on 1 Dec 2020, 08:18 last edited by
          #17

          @KroMignon Thanks for your help, will try it now. I just have question on your translation class. Where do you emit the signal languageChanged and how do you link it to your function which retranslate the app with the engine ? Thanks !

          J 1 Reply Last reply 1 Dec 2020, 08:21
          0
          • K kmarconi
            1 Dec 2020, 08:18

            @KroMignon Thanks for your help, will try it now. I just have question on your translation class. Where do you emit the signal languageChanged and how do you link it to your function which retranslate the app with the engine ? Thanks !

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 1 Dec 2020, 08:21 last edited by
            #18

            @kmarconi what @KroMignon described was the way you had to go before Qt 5.10

            you can take a look at this blog post, I think its rather good:

            https://retifrav.github.io/blog/2017/01/04/translating-qml-app/


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            K 1 Reply Last reply 1 Dec 2020, 08:54
            1
            • J J.Hilk
              1 Dec 2020, 08:21

              @kmarconi what @KroMignon described was the way you had to go before Qt 5.10

              you can take a look at this blog post, I think its rather good:

              https://retifrav.github.io/blog/2017/01/04/translating-qml-app/

              K Offline
              K Offline
              kmarconi
              wrote on 1 Dec 2020, 08:54 last edited by
              #19

              @J-Hilk Yeah I've seen this post but it's kinda dirty way to retranslate the whole app I think ... I still will try it but I hope to find a way to use the retranslate function properly.

              J 1 Reply Last reply 1 Dec 2020, 09:08
              0
              • K kmarconi
                1 Dec 2020, 08:54

                @J-Hilk Yeah I've seen this post but it's kinda dirty way to retranslate the whole app I think ... I still will try it but I hope to find a way to use the retranslate function properly.

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 1 Dec 2020, 09:08 last edited by
                #20

                @kmarconi
                what would you consider a proper retranslate functionality ?

                You can either,

                • call retranslate() on the QQmlEngine,
                • update the strings via the empty string property workaround
                • reload the qml file entirely

                there are not other ways, at least that I know of


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                K 1 Reply Last reply 1 Dec 2020, 09:18
                0
                • J J.Hilk
                  1 Dec 2020, 09:08

                  @kmarconi
                  what would you consider a proper retranslate functionality ?

                  You can either,

                  • call retranslate() on the QQmlEngine,
                  • update the strings via the empty string property workaround
                  • reload the qml file entirely

                  there are not other ways, at least that I know of

                  K Offline
                  K Offline
                  kmarconi
                  wrote on 1 Dec 2020, 09:18 last edited by kmarconi 12 Jan 2020, 09:21
                  #21

                  @J-Hilk Calling retranslate for me is the cleanest way. No need to add "+trans.emptyString" on each of my qsTr object and I have a lot. I need to figure out how to solve this infinite binding loop. Is reloading the whole qml a heavy task for the system ?

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    KroMignon
                    wrote on 1 Dec 2020, 09:20 last edited by KroMignon 12 Jan 2020, 09:20
                    #22

                    @kmarconi said in How can I get the application QmlEngine ?:

                    I still will try it but I hope to find a way to use the retranslate function properly.

                    I can not figure out what your problem is.
                    @J-Hilk have give you all information you need to do it.

                    If you are using Qt 5.10 and upper (which is not my case) simply follow documentation
                    That's not so complicated to follow, you could create a class which handle the locales, for example let it call LocalHandler, when you have install the new translator with QCoreApplication::installTranslator() then emit a signal for example called translationUpated().
                    Then in your main.cpp:

                    LocalHandler loc;
                    
                    QObject::connect(&loc, &LocalHandler::translationUpated, engine, &QQmlEngine::retranslate);
                    

                    Alternatively you can use a app->installEventFilter() to catch QEvent::LanguageChange and then trigger QQmlEngine::retranslate().

                    For prior version, you have to do more work ==> https://retifrav.github.io/blog/2017/01/04/translating-qml-app/

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

                    K 1 Reply Last reply 1 Dec 2020, 09:33
                    1
                    • K KroMignon
                      1 Dec 2020, 09:20

                      @kmarconi said in How can I get the application QmlEngine ?:

                      I still will try it but I hope to find a way to use the retranslate function properly.

                      I can not figure out what your problem is.
                      @J-Hilk have give you all information you need to do it.

                      If you are using Qt 5.10 and upper (which is not my case) simply follow documentation
                      That's not so complicated to follow, you could create a class which handle the locales, for example let it call LocalHandler, when you have install the new translator with QCoreApplication::installTranslator() then emit a signal for example called translationUpated().
                      Then in your main.cpp:

                      LocalHandler loc;
                      
                      QObject::connect(&loc, &LocalHandler::translationUpated, engine, &QQmlEngine::retranslate);
                      

                      Alternatively you can use a app->installEventFilter() to catch QEvent::LanguageChange and then trigger QQmlEngine::retranslate().

                      For prior version, you have to do more work ==> https://retifrav.github.io/blog/2017/01/04/translating-qml-app/

                      K Offline
                      K Offline
                      kmarconi
                      wrote on 1 Dec 2020, 09:33 last edited by
                      #23

                      @KroMignon I already done everything you told me but thank you ;) I was just saying that if I could not use the emptyString trick it could be interesting for me. Why I'm saying this is because with the trick , I have the error " ListElement: cannot use script for property value" . Is is because I cannot use the trick in a ListElement object ?

                      K 1 Reply Last reply 1 Dec 2020, 09:47
                      0
                      • K kmarconi
                        1 Dec 2020, 09:33

                        @KroMignon I already done everything you told me but thank you ;) I was just saying that if I could not use the emptyString trick it could be interesting for me. Why I'm saying this is because with the trick , I have the error " ListElement: cannot use script for property value" . Is is because I cannot use the trick in a ListElement object ?

                        K Offline
                        K Offline
                        KroMignon
                        wrote on 1 Dec 2020, 09:47 last edited by KroMignon 12 Jan 2020, 09:53
                        #24

                        @kmarconi said in How can I get the application QmlEngine ?:

                        I have the error "ListElement: cannot use script for property value"

                        AFAIK, ListElement cannot be changed dynamically ==> cf QTBUG-16289

                        edit

                        You have to use QT_TR_NOOP() and qsTr() on the delegate, like this (extract from QTBUG-16289):

                        ListModel {
                            id: myModel
                        
                            ListElement {
                                desc: QT_TR_NOOP("Example")
                            }
                        }
                        
                        ListView {
                            model: myModel
                        
                            delegate: Text {
                                text: qsTr(desc)
                            }
                        }
                        

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

                        K 1 Reply Last reply 1 Dec 2020, 09:51
                        0
                        • K KroMignon
                          1 Dec 2020, 09:47

                          @kmarconi said in How can I get the application QmlEngine ?:

                          I have the error "ListElement: cannot use script for property value"

                          AFAIK, ListElement cannot be changed dynamically ==> cf QTBUG-16289

                          edit

                          You have to use QT_TR_NOOP() and qsTr() on the delegate, like this (extract from QTBUG-16289):

                          ListModel {
                              id: myModel
                          
                              ListElement {
                                  desc: QT_TR_NOOP("Example")
                              }
                          }
                          
                          ListView {
                              model: myModel
                          
                              delegate: Text {
                                  text: qsTr(desc)
                              }
                          }
                          
                          K Offline
                          K Offline
                          kmarconi
                          wrote on 1 Dec 2020, 09:51 last edited by kmarconi 12 Jan 2020, 09:51
                          #25

                          @KroMignon Yeah just seen it on a stackoverflow post (https://stackoverflow.com/questions/7659442/listelement-fields-as-properties) but I canno't change my qml part because the way it is designed is fixed so I need to find a way to use the retranslate function without having this binding loop.

                          K 1 Reply Last reply 1 Dec 2020, 09:55
                          0
                          • K kmarconi
                            1 Dec 2020, 09:51

                            @KroMignon Yeah just seen it on a stackoverflow post (https://stackoverflow.com/questions/7659442/listelement-fields-as-properties) but I canno't change my qml part because the way it is designed is fixed so I need to find a way to use the retranslate function without having this binding loop.

                            K Offline
                            K Offline
                            KroMignon
                            wrote on 1 Dec 2020, 09:55 last edited by KroMignon 12 Jan 2020, 09:55
                            #26

                            @kmarconi said in How can I get the application QmlEngine ?:

                            I need to find a way to use the retranslate function without having this binding loop.

                            Take a look at the QTBUG-16289, the way they propose is to use QT_TR_NOOP() on ListElement to add string in translation file, and to use qsTr() in the delegate to do translation.

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

                            K 1 Reply Last reply 1 Dec 2020, 14:06
                            2
                            • K KroMignon
                              1 Dec 2020, 09:55

                              @kmarconi said in How can I get the application QmlEngine ?:

                              I need to find a way to use the retranslate function without having this binding loop.

                              Take a look at the QTBUG-16289, the way they propose is to use QT_TR_NOOP() on ListElement to add string in translation file, and to use qsTr() in the delegate to do translation.

                              K Offline
                              K Offline
                              kmarconi
                              wrote on 1 Dec 2020, 14:06 last edited by
                              #27

                              @KroMignon Damn it is so hard to debug a binding loop with QT. Will keep you updated if I finally found out what's going on with my program.

                              K 1 Reply Last reply 1 Dec 2020, 14:13
                              0
                              • K kmarconi
                                1 Dec 2020, 14:06

                                @KroMignon Damn it is so hard to debug a binding loop with QT. Will keep you updated if I finally found out what's going on with my program.

                                K Offline
                                K Offline
                                KroMignon
                                wrote on 1 Dec 2020, 14:13 last edited by
                                #28

                                @kmarconi said in How can I get the application QmlEngine ?:

                                Damn it is so hard to debug a binding loop with QT.

                                Yes, binding loops are the hell!
                                Which Qt Version are you using? For Qt 5.10 and upper you can enable tracing to help you to find bindings problems with QT_LOGGING_RULES.
                                For example in your main.cpp:

                                    qputenv("QT_LOGGING_RULES", "qt.qml.binding.removal.info=true");
                                

                                Take look at this https://www.kdab.com/new-qt-5-10-diagnostics-breaking-qml-bindings/ for more details.

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

                                K 2 Replies Last reply 1 Dec 2020, 14:22
                                1
                                • K KroMignon
                                  1 Dec 2020, 14:13

                                  @kmarconi said in How can I get the application QmlEngine ?:

                                  Damn it is so hard to debug a binding loop with QT.

                                  Yes, binding loops are the hell!
                                  Which Qt Version are you using? For Qt 5.10 and upper you can enable tracing to help you to find bindings problems with QT_LOGGING_RULES.
                                  For example in your main.cpp:

                                      qputenv("QT_LOGGING_RULES", "qt.qml.binding.removal.info=true");
                                  

                                  Take look at this https://www.kdab.com/new-qt-5-10-diagnostics-breaking-qml-bindings/ for more details.

                                  K Offline
                                  K Offline
                                  kmarconi
                                  wrote on 1 Dec 2020, 14:22 last edited by
                                  #29

                                  @KroMignon I'm on QT 5.12.3 so that could save me. Will tell you, thanks !

                                  1 Reply Last reply
                                  0
                                  • K KroMignon
                                    1 Dec 2020, 14:13

                                    @kmarconi said in How can I get the application QmlEngine ?:

                                    Damn it is so hard to debug a binding loop with QT.

                                    Yes, binding loops are the hell!
                                    Which Qt Version are you using? For Qt 5.10 and upper you can enable tracing to help you to find bindings problems with QT_LOGGING_RULES.
                                    For example in your main.cpp:

                                        qputenv("QT_LOGGING_RULES", "qt.qml.binding.removal.info=true");
                                    

                                    Take look at this https://www.kdab.com/new-qt-5-10-diagnostics-breaking-qml-bindings/ for more details.

                                    K Offline
                                    K Offline
                                    kmarconi
                                    wrote on 1 Dec 2020, 14:53 last edited by
                                    #30

                                    @KroMignon My binding loop error hasn't changed at all even with "qputenv("QT_LOGGING_RULES", "qt.qml.binding.removal.info=true");" in my main.cpp file .. Am I condemned to live in this infinite binding loop ? haha

                                    K 1 Reply Last reply 1 Dec 2020, 14:59
                                    0
                                    • K kmarconi
                                      1 Dec 2020, 14:53

                                      @KroMignon My binding loop error hasn't changed at all even with "qputenv("QT_LOGGING_RULES", "qt.qml.binding.removal.info=true");" in my main.cpp file .. Am I condemned to live in this infinite binding loop ? haha

                                      K Offline
                                      K Offline
                                      KroMignon
                                      wrote on 1 Dec 2020, 14:59 last edited by
                                      #31

                                      @kmarconi said in How can I get the application QmlEngine ?:

                                      Am I condemned to live in this infinite binding loop ?

                                      Can you show the QML code which is not working well?

                                      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
                                      0
                                      • K kmarconi
                                        30 Nov 2020, 14:08

                                        Here is my main.qml file :

                                        Item {
                                        id:root
                                        visible: true
                                        width: 640
                                        height: 480

                                        property var mainMenu: Menu{rootStack: root}
                                        
                                        StackView {
                                            anchors.fill: parent
                                            id: menuStack
                                        
                                            property var button: hmiclass.button
                                            initialItem: mainMenu
                                        
                                            onButtonChanged: {
                                                mainMenu.onButtonPushed(button)
                                            }
                                        }
                                        

                                        }

                                        EDIT: I need to find where that loop is coming from.. Or to find a patch to refresh only the bindings which are in the translation file.

                                        K Offline
                                        K Offline
                                        kmarconi
                                        wrote on 1 Dec 2020, 15:09 last edited by
                                        #32

                                        @kmarconi said in How can I get the application QmlEngine ?:

                                        Here is my main.qml file :

                                        Item {
                                        id:root
                                        visible: true
                                        width: 640
                                        height: 480

                                        property var mainMenu: Menu{rootStack: root}
                                        
                                        StackView {
                                            anchors.fill: parent
                                            id: menuStack
                                        
                                            property var button: hmiclass.button
                                            initialItem: mainMenu
                                        
                                            onButtonChanged: {
                                                mainMenu.onButtonPushed(button)
                                            }
                                        }
                                        

                                        }

                                        EDIT: I need to find where that loop is coming from.. Or to find a patch to refresh only the bindings which are in the translation file.

                                        This is the one

                                        K 1 Reply Last reply 1 Dec 2020, 15:16
                                        0
                                        • K kmarconi
                                          1 Dec 2020, 15:09

                                          @kmarconi said in How can I get the application QmlEngine ?:

                                          Here is my main.qml file :

                                          Item {
                                          id:root
                                          visible: true
                                          width: 640
                                          height: 480

                                          property var mainMenu: Menu{rootStack: root}
                                          
                                          StackView {
                                              anchors.fill: parent
                                              id: menuStack
                                          
                                              property var button: hmiclass.button
                                              initialItem: mainMenu
                                          
                                              onButtonChanged: {
                                                  mainMenu.onButtonPushed(button)
                                              }
                                          }
                                          

                                          }

                                          EDIT: I need to find where that loop is coming from.. Or to find a patch to refresh only the bindings which are in the translation file.

                                          This is the one

                                          K Offline
                                          K Offline
                                          KroMignon
                                          wrote on 1 Dec 2020, 15:16 last edited by
                                          #33

                                          @kmarconi And what is the binding loop error message? I can't see any obvious error on this code extract.

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

                                          K 1 Reply Last reply 1 Dec 2020, 15:53
                                          0

                                          23/38

                                          1 Dec 2020, 09:33

                                          • Login

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