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. Connecting the global class to QML will make my app crash on my android
Forum Updated to NodeBB v4.3 + New Features

Connecting the global class to QML will make my app crash on my android

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 760 Views 1 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.
  • zenghsingZ Offline
    zenghsingZ Offline
    zenghsing
    wrote on last edited by
    #1

    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.height

    Component.onCompleted:
    {
    zhGlobalClass.setScreenSize( win_root.width, win_root.height )
    zhGlobalClass.remove_template_file()
    }
    Connections{
    target: zhGlobalClass //<--------
    }
    ....

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      4
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #3

        You must not create QObjects before your QApplication has been created.

        1 Reply Last reply
        1

        • Login

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