Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Keep created object alive. How?
Qt 6.11 is out! See what's new in the release blog

Keep created object alive. How?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.6k 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by bogong
    #1

    Hello all!
    I am running static function from one class that should create some objects and attach it to main function. How to do it in Qt?

    main.cpp

    int main(int Counter, char *Arguments[]) {
    
    	QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    	CustomApplication Application(Counter,Arguments);
    
    	QQmlApplicationEngine Engine;
    
    	>>>> CustomClass::staticFunction(&Engine)
    
    	Engine.load(QUrl(Main));
    	if (Engine.rootObjects().isEmpty())
    		return -1;
    
    	return Application.exec();
    

    customclass.cpp

    void CustomClass::staticFunction(QQmlApplicationEngine &Engine) {
    
    	QQmlContext *Context = Engine.rootContext();
    
    	Settings appSettings;
    	Context->setContextProperty("Settings",&appSettings);
    }
    

    In this example object appSettings should be alive in main function scope after finishing of execution of this static function. Beside this the list of objects might be bigger.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Allocate on the heap and give ownership of the memory to something else (Engine?)

      Settings* appSettings = new Settings;
      QObject::connect(&Engine,&QObject::destroyed,[appSettings](){delete appSettings;});
      Context->setContextProperty("Settings",appSettings);
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      B 1 Reply Last reply
      4
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Or simply let Settings derive from QObject and pass Engine as parent so it will automatically destroyed when Engine is deleted.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        B 1 Reply Last reply
        2
        • VRoninV VRonin

          Allocate on the heap and give ownership of the memory to something else (Engine?)

          Settings* appSettings = new Settings;
          QObject::connect(&Engine,&QObject::destroyed,[appSettings](){delete appSettings;});
          Context->setContextProperty("Settings",appSettings);
          
          B Offline
          B Offline
          bogong
          wrote on last edited by bogong
          #4

          @VRonin Engine is object that is conaining context. I need to hold Settings standalone in main, but be contexted to Engine. Global idea - create standalone objects from static function, keep them alive when function finished and hold them in context of Engine.

          Thanks for reply. I am always trying to avoid HEAP. In this case it's might be better to use kind of object-wrapper and in constructor of it do everything I need and keep it alive in main by direct implementation.

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Or simply let Settings derive from QObject and pass Engine as parent so it will automatically destroyed when Engine is deleted.

            B Offline
            B Offline
            bogong
            wrote on last edited by bogong
            #5

            @Christian-Ehrlicher Will try to do it in hour. But the global idea was to develop them like standalone object but inside of main.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bogong
              wrote on last edited by bogong
              #6

              Decided do not making additional complexity. The solution is - do not do anything additional and use plain objects definition inside of "main" function.

              Qt Project example published in Github

              main.cpp

              int main(int Counter, char *Arguments[]) {
              
              	QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
              	CuxtomApplication Application(Counter,Arguments);
              
              	QQmlApplicationEngine Engine;
              	QQmlContext *Context = Engine.rootContext();
              
              	AppSettings Settings;
              	Context->setContextProperty("Settings",&Settings);
              
              	Engine.load(QUrl(Main));
              	if (Engine.rootObjects().isEmpty())
              		return -1;
              
              	return Application.exec();
              }
              
              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