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. QQmlApplicationEngine in singleton
Forum Updated to NodeBB v4.3 + New Features

QQmlApplicationEngine in singleton

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 851 Views 2 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.
  • D Offline
    D Offline
    DimkaMk
    wrote on last edited by
    #1

    Why when I close application i get Segmentation filed ???

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    #include "test.h"
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        Test::instance().init();
        return app.exec();
    }
    

    test.h

    #include <QtQuick>
    
    
    class Test
    {
        QQmlApplicationEngine engine;
    
    public:
        void init();
        static Test &instance();
    
    };
    

    test.cpp

    #include <QtQuick>
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    void Test::init()
    {
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    }
    
    Test &Test::instance()
    {
        static Test test;
        return test;
    }
    
    1 Reply Last reply
    0
    • jpnurmiJ Offline
      jpnurmiJ Offline
      jpnurmi
      wrote on last edited by
      #2

      It doesn't sound like a good idea to have a QML engine instance outlive the QGuiApplication instance. Better destroy the QML engine before returning from main(). You could, for example, allocate the engine on the heap and add something like cleanup() as a pair to init(), or allocate the engine on the stack in main() and pass it as an argument to init().

      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