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. Initialization and cleanup
QtWS25 Last Chance

Initialization and cleanup

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.0k Views
  • 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.
  • W Offline
    W Offline
    WhatIf
    wrote on last edited by
    #1

    I designed my app using Qt Designer. Where should I place the code for initialization and cleanup (best practice)? For example, where should establishing a network connection or database be and where should the clean up for those connections be.

    kshegunovK 1 Reply Last reply
    0
    • W WhatIf

      I designed my app using Qt Designer. Where should I place the code for initialization and cleanup (best practice)? For example, where should establishing a network connection or database be and where should the clean up for those connections be.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @WhatIf
      Hello,

      Short answer:
      Where they're used.

      Longer answer:
      When acquiring a resource (as a database connection) it a good idea to have that same class manage and free the resource - the ever-so-relevant RAII principle. Basically, every resource that you create and initialize, you free as well. Ordinarily, you wouldn't transfer the ownership of the resource, but there are exceptions. The resource you manage is usually exposed through the class' interface (the methods), so others can manipulate it.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • W Offline
        W Offline
        WhatIf
        wrote on last edited by
        #3

        What I was trying to get to is, for example, JSP has jspInit() for initialization and jspDestroy() for clean up. Does Qt have a similar approach to initialize code that will be used through out the life of the program and in different classes? Where should I do final clean up before the program ends?

        kshegunovK 1 Reply Last reply
        0
        • W WhatIf

          What I was trying to get to is, for example, JSP has jspInit() for initialization and jspDestroy() for clean up. Does Qt have a similar approach to initialize code that will be used through out the life of the program and in different classes? Where should I do final clean up before the program ends?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @WhatIf

          Does Qt have a similar approach to initialize code that will be used through out the life of the program and in different classes?

          Nothing to do with Qt actually. No, there is no special place for that. You initialize in main(), run your code in main(), and then clean up in main(). Or, since you're using an object-oriented language (i.e. C++), you create a few objects in main() and whatever resources they acquire in their constructors (or along the way), they clean up in the destructors (this is what RAII means). The only thing left to be done in main() is to clean those few objects if needed.

          Where should I do final clean up before the program ends?

          Before returning from main().

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Just a small correction, in order to use Qt's features like signals and slots you first need to create an instance of QCoreApplication and for GUI application depending on the type either a QGuiApplication or a QApplication.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            kshegunovK 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Just a small correction, in order to use Qt's features like signals and slots you first need to create an instance of QCoreApplication and for GUI application depending on the type either a QGuiApplication or a QApplication.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #6

              @SGaist, @WhatIf
              Ah, yes, I missed to mentioned that. The first QObject one creates should be the root QObject, that is one of the QCoreApplication hierarchy (with some very minor and irrelevant for this discussion exceptions). Still, one does create the application object in main(). :D

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0

              • Login

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