How can I get the application QmlEngine ?
-
@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/
wrote on 1 Dec 2020, 08:54 last edited by@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-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.
@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
-
@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
wrote on 1 Dec 2020, 09:18 last edited by kmarconi 12 Jan 2020, 09: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 ?
-
wrote on 1 Dec 2020, 09:20 last edited by KroMignon 12 Jan 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 withQCoreApplication::installTranslator()
then emit a signal for example calledtranslationUpated()
.
Then in your main.cpp:LocalHandler loc; QObject::connect(&loc, &LocalHandler::translationUpated, engine, &QQmlEngine::retranslate);
Alternatively you can use a
app->installEventFilter()
to catchQEvent::LanguageChange
and then triggerQQmlEngine::retranslate()
.For prior version, you have to do more work ==> https://retifrav.github.io/blog/2017/01/04/translating-qml-app/
-
@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 withQCoreApplication::installTranslator()
then emit a signal for example calledtranslationUpated()
.
Then in your main.cpp:LocalHandler loc; QObject::connect(&loc, &LocalHandler::translationUpated, engine, &QQmlEngine::retranslate);
Alternatively you can use a
app->installEventFilter()
to catchQEvent::LanguageChange
and then triggerQQmlEngine::retranslate()
.For prior version, you have to do more work ==> https://retifrav.github.io/blog/2017/01/04/translating-qml-app/
wrote on 1 Dec 2020, 09:33 last edited by@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 ?
-
@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 ?
wrote on 1 Dec 2020, 09:47 last edited by KroMignon 12 Jan 2020, 09:53@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()
andqsTr()
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) } }
-
@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()
andqsTr()
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) } }
wrote on 1 Dec 2020, 09:51 last edited by kmarconi 12 Jan 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.
-
@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.
wrote on 1 Dec 2020, 09:55 last edited by KroMignon 12 Jan 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 useqsTr()
in the delegate to do translation. -
@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 useqsTr()
in the delegate to do translation.wrote on 1 Dec 2020, 14:06 last edited by@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.
-
@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.
wrote on 1 Dec 2020, 14:13 last edited by@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 withQT_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.
-
@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 withQT_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.
wrote on 1 Dec 2020, 14:22 last edited by@KroMignon I'm on QT 5.12.3 so that could save me. Will tell you, thanks !
-
@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 withQT_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.
wrote on 1 Dec 2020, 14:53 last edited by@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
-
@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
wrote on 1 Dec 2020, 14:59 last edited by@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?
-
Here is my main.qml file :
Item {
id:root
visible: true
width: 640
height: 480property 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.
wrote on 1 Dec 2020, 15:09 last edited by@kmarconi said in How can I get the application QmlEngine ?:
Here is my main.qml file :
Item {
id:root
visible: true
width: 640
height: 480property 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
-
@kmarconi said in How can I get the application QmlEngine ?:
Here is my main.qml file :
Item {
id:root
visible: true
width: 640
height: 480property 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
-
@kmarconi And what is the binding loop error message? I can't see any obvious error on this code extract.
wrote on 1 Dec 2020, 15:53 last edited by@KroMignon Sorry, the error is : QML StackView: Binding loop detected for property "button"
-
@KroMignon Sorry, the error is : QML StackView: Binding loop detected for property "button"
wrote on 1 Dec 2020, 16:00 last edited by@kmarconi said in How can I get the application QmlEngine ?:
the error is : QML StackView: Binding loop detected for property "button"
According to your code, it looks like
mainMenu.onButtunPushed()
will changehmiclass.button
content, which will recallmainMenu.onButtunPushed()
... -
@kmarconi said in How can I get the application QmlEngine ?:
the error is : QML StackView: Binding loop detected for property "button"
According to your code, it looks like
mainMenu.onButtunPushed()
will changehmiclass.button
content, which will recallmainMenu.onButtunPushed()
...wrote on 1 Dec 2020, 16:04 last edited by@KroMignon Yeah that's what I tought but why this error is only showing on retranslate and not on the app boot too ?
-
@KroMignon Yeah that's what I tought but why this error is only showing on retranslate and not on the app boot too ?
wrote on 1 Dec 2020, 16:12 last edited by@kmarconi said in How can I get the application QmlEngine ?:
Yeah that's what I tought but why this error is only showing on retranslate and not on the app boot too ?
Because there have to be a first change to start the loop!
I don't know what is changinghmiclass.button
?I suppose
hmiclass
is a C++ class instance andbutton
is a property of this class.
In some way the attached signal to this property is emitted, which will start the loop.That is just an hypothesis, I don't have a clue about your code.
-
@kmarconi said in How can I get the application QmlEngine ?:
Yeah that's what I tought but why this error is only showing on retranslate and not on the app boot too ?
Because there have to be a first change to start the loop!
I don't know what is changinghmiclass.button
?I suppose
hmiclass
is a C++ class instance andbutton
is a property of this class.
In some way the attached signal to this property is emitted, which will start the loop.That is just an hypothesis, I don't have a clue about your code.
wrote on 1 Dec 2020, 16:19 last edited by@KroMignon Yeah sorry I know that I'm asking a lot of help without any clue of my code. But I canno't share more information about it, I'm sorry... Will try to solve my problem with what you gave me today and will keep you updated. Thanks a lot
28/38