How can I get the application QmlEngine ?
-
Hi everyone,
I'm posting here because I'm stuck on a problem of getting the engine of the main application in other classes. For the moment, I've tried several method like :
QQmlApplicationEngine* engine = qobject_cast<QQmlApplicationEngine*>(qmlEngine(this));
QQmlEngine * engine = qmlEngine(this);
QQmlEngine* engine = QQmlEngine::contextForObject(this)->engine();Why I want this engine is because I need a class to restranslate my app when I want it. For the moment, none of these ideas worked and either they are making the app unable to start nor the app is crashing when calling the function to retranslate (engine->retranslate())
If you have any idea on how could I successfully use the main engine of my app in other classes, I would be glad to hear it.
Thanks !
-
@kmarconi said in How can I get the application QmlEngine ?:
QQmlApplicationEngine
As of right now, I do not know of an easy way to get the engine without passing it on via reference or pointer.
what's this supposed to be actually ?
qobject_cast<QQmlApplicationEngine*>(qmlEngine(this));
qmlEngine(this);
Why don't you forward a signal to your main.cpp and simply connect there to retranslate?
-
What I do actually is passing a reference of the engine into my class of translation. But since I never create an engine, could it be the reason why it is not working ?
what's this supposed to be actually ?
qobject_cast<QQmlApplicationEngine*>(qmlEngine(this));
This is one of the way I found to get the engine.
The problem here I think is that in each example I see which are restranlating their apps, they are using only one cpp file and one qml file so they just simply create an engine in the main and load the qml in it. But for me, it's not possible because I have a lot of classes and since one of them is already using my "main.qml" file (its a class heriting from QQuickView which use the qml file with the function "SetSource"), I canno't create another engine to load the qml file because it is making my app crash. I'm pretty sure that what I just wrote is not clear so don't hesitate don't ask me whatever you want.EDIT: If I try to qDebug my engine, I see : QObject(0x0) which means that my problem is with my way to get the engine.
Thanks for your quick answer !
-
@J-Hilk Nop, sorry. I have for the moment none QQmlEngines inside my project. To display my app, I have a class heriting from QQuickView which is using OpenGL to display my app via the function myView.SetSources("qrc:/main.qml"). After some googling, every example I found for dynamic translation are done in three steps : remove the actual QTranslator, install the new one and then retranslate the app. But in each of these examples, they are doing this line : engine->load(QUrl("qrc:/main.qml")) at first. If i do this line, my app is immediatly crashing because the qml file is already in use in the class which display the app. Let's say this class's name is "myView", I also tried to get the engine by doing : QQmlEngine* engine = QQmlEngine::contextForObject(&myView)->engine(); but my app is directly crashing.
-
@kmarconi ok I get it,
QQuickView has a getter for the internal QQmlEngine
https://doc.qt.io/qt-5/qquickview.html#enginecall that and simply call retranslate on that returned object, and you should be fine
-
@J-Hilk Wow I think you saved my life here, thanks ^^ My app is finally not crashing anymore but I have now an error from my main.qml file : QML StackView: Binding loop detected for property "button". Any idea on why this could happen ?
Big thanks.
-
@kmarconi hard to tell without knowing your qml file content,
retranslate does not only reevaluate your text bindings, but all bindings
https://doc.qt.io/qt-5/qqmlengine.html#retranslateNote: Due to a limitation in the implementation, this function refreshes all the engine's bindings, not only those that use strings marked for translation. This may be optimized in a future release.
so, the bug is probably there from the binging :D
-
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.
-
@kmarconi
take a look here:
https://doc.qt.io/qt-5/qtquick-internationalization.html -
@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
😉
-
@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 withengine->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.
-
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.
-
@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 !
-
@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/
-
@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