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. Correct startup of program
QtWS25 Last Chance

Correct startup of program

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 357 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.
  • B Offline
    B Offline
    buzz_lightzyear
    wrote on last edited by
    #1

    Hi! :-)

    I have implemented a little program with an SQL Lite database connection. My main function looks like this:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        DBConnection *dbc = DBConnection::getInstance(); //Singleton implementation
        dbc->openConnection();
        delete dbc;
    
        return a.exec();
    }
    

    And there is the problem... the dbc-object will be deleted immediately.

    Can someone give me advice how I should implement that correctly?

    Thx in advance :-)

    bl

    JonBJ Pl45m4P 2 Replies Last reply
    0
    • B buzz_lightzyear

      Hi! :-)

      I have implemented a little program with an SQL Lite database connection. My main function looks like this:

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
      
          DBConnection *dbc = DBConnection::getInstance(); //Singleton implementation
          dbc->openConnection();
          delete dbc;
      
          return a.exec();
      }
      

      And there is the problem... the dbc-object will be deleted immediately.

      Can someone give me advice how I should implement that correctly?

      Thx in advance :-)

      bl

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @buzz_lightzyear
      Well of course, because you delete it immediately!

      If you want to keep the database/connection around for re-use, don't delete it. If it's a singleton implementation you don't even need to keep the variable accessible since you can always call DBConnection::getInstance() again when you want it.

      Quite what your DBConnection is I do not know. Normally for SQLite you would use Qt's QSqlDatabase et al classes.

      1 Reply Last reply
      3
      • B buzz_lightzyear

        Hi! :-)

        I have implemented a little program with an SQL Lite database connection. My main function looks like this:

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
        
            DBConnection *dbc = DBConnection::getInstance(); //Singleton implementation
            dbc->openConnection();
            delete dbc;
        
            return a.exec();
        }
        

        And there is the problem... the dbc-object will be deleted immediately.

        Can someone give me advice how I should implement that correctly?

        Thx in advance :-)

        bl

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @buzz_lightzyear said in Correct startup of program:

        And there is the problem... the dbc-object will be deleted immediately.

        Because you open and delete it immediately.

        What kind of class is DBConnection? Do you really need it?
        QSqlDatabase and its static functions wont work for you?

        Edit:
        @JonB :D


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        2
        • B Offline
          B Offline
          buzz_lightzyear
          wrote on last edited by
          #4

          Thx for your answers, I think I have told my problem wrong.

          I think my problem is this line:

              return a.exec();
          

          All stuff I want to do I have to do before this line, also delete my database connection with

          delete dbc;
          

          Now I have made some changes to my code like this:

              a.exec();
              delete dbc;
              return 0;
          

          Is this ok to do it this way? Or can that lead to any other problems?

          Thank you :-)

          jsulmJ Christian EhrlicherC 2 Replies Last reply
          0
          • B buzz_lightzyear

            Thx for your answers, I think I have told my problem wrong.

            I think my problem is this line:

                return a.exec();
            

            All stuff I want to do I have to do before this line, also delete my database connection with

            delete dbc;
            

            Now I have made some changes to my code like this:

                a.exec();
                delete dbc;
                return 0;
            

            Is this ok to do it this way? Or can that lead to any other problems?

            Thank you :-)

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @buzz_lightzyear said in Correct startup of program:

            Is this ok to do it this way?

            yes

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • B buzz_lightzyear

              Thx for your answers, I think I have told my problem wrong.

              I think my problem is this line:

                  return a.exec();
              

              All stuff I want to do I have to do before this line, also delete my database connection with

              delete dbc;
              

              Now I have made some changes to my code like this:

                  a.exec();
                  delete dbc;
                  return 0;
              

              Is this ok to do it this way? Or can that lead to any other problems?

              Thank you :-)

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @buzz_lightzyear said in Correct startup of program:

              auto ret = a.exec();
              delete dbc;
              return ret;
              

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

              1 Reply Last reply
              3
              • B Offline
                B Offline
                buzz_lightzyear
                wrote on last edited by
                #7

                Thank you all for your help! :-)

                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