Unsolved Connecting the global class to QML will make my app crash on my android
-
I want to use my global class in cpp and qml.
But it will make my app crash on android device.I find it work fine when I create the instance in main function like the
comment in below code . But I want to define global scope variable.What should I do when I want to connect the global class to QML?
main.cpp
#include "zh_global_class.h"
zhGlobalClass g_class; //<--------
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlContext *ctext=engine.rootContext();ctext->setContextProperty("zhGlobalClass", &g_class);
//<--------
// zhGlobalClass g_class2;
// ctext->setContextProperty("zhGlobalClass", &g_class2);
....main.qml
Window {id: win_root
visible:true
visibility: Window.FullScreen
width:Screen.width
height:Screen.heightComponent.onCompleted:
{
zhGlobalClass.setScreenSize( win_root.width, win_root.height )
zhGlobalClass.remove_template_file()
}
Connections{
target: zhGlobalClass //<--------
}
.... -
I'm not sure about your use case. I had similar use case. Instead of making the global object , I made it as global pointer and created object inside the main function.
Issue is coming because global object is created in different thread context than the QML engine thread context. This is causing the issue on Android. This works on desktop version.
-
You must not create
QObjects
before yourQApplication
has been created.