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. Interrupt startup / abort programm start in main()
Forum Updated to NodeBB v4.3 + New Features

Interrupt startup / abort programm start in main()

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 3.7k 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
    Dominic Jonas
    wrote on last edited by
    #1

    I want to check at the programm start if the user has got a licence. Otherwise the programm should close immediately.

    Here is an excerpt from the code.

    int main(int argc, char *argv[])
    {
        QGuiApplication a(argc, argv);
        QCoreApplication::setApplicationName("App name");
        QCoreApplication::setApplicationVersion(APP_VERSION);
        QCoreApplication::setOrganizationName("organ");
        QCoreApplication::setOrganizationDomain("organ.de");
    
        if(!Licence::IsLicenced())
        {
            qInfo() << "Missing Licence - ShutDown App!";
            QCoreApplication::quit();
        }
        else qInfo() << "License accepted!";
    
        // Initialize CLI
        CliHandler cliHandler;
        QtCli::init();
        QtCli::registerHandler(&cliHandler, "handle");
    
        // load config
        Config::instance();
    
        qInfo() << qPrintable("=== " + QCoreApplication::applicationName() + " Version " + QCoreApplication::applicationVersion() + " ===");
    
        *Removed code*
    
        QQuickView view;
        view.engine()->addImageProvider("componentpictures", new PictureProvider);
        view.setResizeMode(QQuickView::SizeRootObjectToView);
        view.setSource(QUrl("qrc:/qml/main.qml"));
        view.show();
    
        // send a discover event
        udpHandler->sendDiscoverEvent();
        return a.exec();
    }
    

    Although it jumps into the shutdown (calls QCoreApplication::quit();), the app is not terminated.

    Someone knows wheres the error?

    jsulmJ 1 Reply Last reply
    0
    • joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #2

      @Dominic-Jonas Hi, friend, welcome qt forum.

      Shared my main for you.

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
      
          if( !w.CheckRegister() ){goto _FAILED;}
      
         /** some code at here. */
      
          return a.exec();
      _FAILED:
          return 0;
      }
      

      Just do it!

      jsulmJ 1 Reply Last reply
      0
      • D Dominic Jonas

        I want to check at the programm start if the user has got a licence. Otherwise the programm should close immediately.

        Here is an excerpt from the code.

        int main(int argc, char *argv[])
        {
            QGuiApplication a(argc, argv);
            QCoreApplication::setApplicationName("App name");
            QCoreApplication::setApplicationVersion(APP_VERSION);
            QCoreApplication::setOrganizationName("organ");
            QCoreApplication::setOrganizationDomain("organ.de");
        
            if(!Licence::IsLicenced())
            {
                qInfo() << "Missing Licence - ShutDown App!";
                QCoreApplication::quit();
            }
            else qInfo() << "License accepted!";
        
            // Initialize CLI
            CliHandler cliHandler;
            QtCli::init();
            QtCli::registerHandler(&cliHandler, "handle");
        
            // load config
            Config::instance();
        
            qInfo() << qPrintable("=== " + QCoreApplication::applicationName() + " Version " + QCoreApplication::applicationVersion() + " ===");
        
            *Removed code*
        
            QQuickView view;
            view.engine()->addImageProvider("componentpictures", new PictureProvider);
            view.setResizeMode(QQuickView::SizeRootObjectToView);
            view.setSource(QUrl("qrc:/qml/main.qml"));
            view.show();
        
            // send a discover event
            udpHandler->sendDiscoverEvent();
            return a.exec();
        }
        

        Although it jumps into the shutdown (calls QCoreApplication::quit();), the app is not terminated.

        Someone knows wheres the error?

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

        @Dominic-Jonas Just move this code at the beginning of main and replace QCoreApplication::quit() with exit(1):

        int main(int argc, char *argv[])
        {
            if(!Licence::IsLicenced())
            {
                qInfo() << "Missing Licence - ShutDown App!";
                exit(1);
            }
        

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

        1 Reply Last reply
        2
        • joeQJ joeQ

          @Dominic-Jonas Hi, friend, welcome qt forum.

          Shared my main for you.

          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              MainWindow w;
              w.show();
          
              if( !w.CheckRegister() ){goto _FAILED;}
          
             /** some code at here. */
          
              return a.exec();
          _FAILED:
              return 0;
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @joeQ Why do you use a goto for this?if .. else is better.

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

          joeQJ 2 Replies Last reply
          1
          • jsulmJ jsulm

            @joeQ Why do you use a goto for this?if .. else is better.

            joeQJ Offline
            joeQJ Offline
            joeQ
            wrote on last edited by
            #5

            @jsulm Just like it. (^o^)/~

            Just do it!

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @joeQ Why do you use a goto for this?if .. else is better.

              joeQJ Offline
              joeQJ Offline
              joeQ
              wrote on last edited by
              #6

              @jsulm I like goto, not like if..else.

              void Fun()
              {
                if(true){goto _OK;}
                if(false){goto _END;}
                if(true){goto _OK;}
                if(false){goto _END;}
                if(true){goto _OK;}
                if(false){goto _END;}
                if(true){goto _OK;}
               /**...*/
              
              _OK:
                return true;
              _END:
                return false;
              }
              
              void Fun()
              {
                if(true)
                {
                   if(true)
                   {
                      return false;
                   }
                   else
                   {
                      if(true)
                      {
                           return true;
                      }
                      return false;
                   }
                   return true;
                }
                else if(true)
                {
                    return true;
                }
                else
               {
                   return false;
                }
              }
              

              so, I like use goto.

              Just do it!

              jsulmJ 1 Reply Last reply
              0
              • joeQJ joeQ

                @jsulm I like goto, not like if..else.

                void Fun()
                {
                  if(true){goto _OK;}
                  if(false){goto _END;}
                  if(true){goto _OK;}
                  if(false){goto _END;}
                  if(true){goto _OK;}
                  if(false){goto _END;}
                  if(true){goto _OK;}
                 /**...*/
                
                _OK:
                  return true;
                _END:
                  return false;
                }
                
                void Fun()
                {
                  if(true)
                  {
                     if(true)
                     {
                        return false;
                     }
                     else
                     {
                        if(true)
                        {
                             return true;
                        }
                        return false;
                     }
                     return true;
                  }
                  else if(true)
                  {
                      return true;
                  }
                  else
                 {
                     return false;
                  }
                }
                

                so, I like use goto.

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

                @joeQ goto is very bad style.
                Your example is bad: if (true) will always evaluate to true...
                But it is up to you how you program.

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

                joeQJ 1 Reply Last reply
                2
                • D Offline
                  D Offline
                  Dominic Jonas
                  wrote on last edited by Dominic Jonas
                  #8

                  @joeQ @jsulm thank you both for your answer.

                  Can be marked as solved!

                  Regards
                  Dominic

                  kshegunovK 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @joeQ goto is very bad style.
                    Your example is bad: if (true) will always evaluate to true...
                    But it is up to you how you program.

                    joeQJ Offline
                    joeQJ Offline
                    joeQ
                    wrote on last edited by
                    #9

                    @jsulm Yes, Many people think goto is bad style.

                    But, I like it. and, if(true) means if(condition), not always return true. use goto, is my personal programming habits. I think the programming habits can help me.

                    many times, use goto is up to program. You are right..

                    This is my Chinese blog about How to use goto. You can see the code example.

                    if else is good. and goto is not bad. Just you can enhanced readability of the program. They are the best.

                    Just do it!

                    jsulmJ 2 Replies Last reply
                    0
                    • joeQJ joeQ

                      @jsulm Yes, Many people think goto is bad style.

                      But, I like it. and, if(true) means if(condition), not always return true. use goto, is my personal programming habits. I think the programming habits can help me.

                      many times, use goto is up to program. You are right..

                      This is my Chinese blog about How to use goto. You can see the code example.

                      if else is good. and goto is not bad. Just you can enhanced readability of the program. They are the best.

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

                      @joeQ I agree there are rare cases a goto can make sense, but in most cases it just creates a confusing mess. Do you know switch(...) statement in C++? In my whole career I cannot remember to have used goto by myself and I cannot remember to have seen my colleagues using it (we do code reviews, so I know what they write). Do you develop software in a team?

                      I cannot check your example as your blog does not load here.

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

                      1 Reply Last reply
                      0
                      • joeQJ joeQ

                        @jsulm Yes, Many people think goto is bad style.

                        But, I like it. and, if(true) means if(condition), not always return true. use goto, is my personal programming habits. I think the programming habits can help me.

                        many times, use goto is up to program. You are right..

                        This is my Chinese blog about How to use goto. You can see the code example.

                        if else is good. and goto is not bad. Just you can enhanced readability of the program. They are the best.

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

                        @joeQ Now it finely loaded. To your example: for this you should use smart pointers in C++, this way you make sure the memory is released (you even do not have to do it manually!). See: http://www.cplusplus.com/reference/memory/unique_ptr/
                        Using smart pointers in your example there would not be any need to delete the memory - it will be done for you as soon as the smart pointers go out of scope.
                        In general, in modern C++ you should avoid to use raw pointers, use smart pointers instead.

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

                        joeQJ 1 Reply Last reply
                        0
                        • D Dominic Jonas

                          @joeQ @jsulm thank you both for your answer.

                          Can be marked as solved!

                          Regards
                          Dominic

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

                          Avoid goto and ::exit() as the plague! The first one is a C remnant that dates back to asm's jmp instruction and has no place in good code, ever! The second one will allow destructors to be called only on some variables (i.e. global ones), everything else is left in a inconsistent state, so the less you use exit the better.

                          The "good" solution here, and in most cases, is to just give up as early as possible by returning the appropriate error code, e.g.:

                          int main(int argc, char ** argv)
                          {
                              QApplication app(argc, argv);
                              if (something_bad)
                                  return 1; // Calling ::exit will not allow QApplication to clean up
                          
                              // ... more code
                              return QApplication::exec();
                          }
                          

                          Read and abide by the Qt Code of Conduct

                          joeQJ 1 Reply Last reply
                          4
                          • jsulmJ jsulm

                            @joeQ Now it finely loaded. To your example: for this you should use smart pointers in C++, this way you make sure the memory is released (you even do not have to do it manually!). See: http://www.cplusplus.com/reference/memory/unique_ptr/
                            Using smart pointers in your example there would not be any need to delete the memory - it will be done for you as soon as the smart pointers go out of scope.
                            In general, in modern C++ you should avoid to use raw pointers, use smart pointers instead.

                            joeQJ Offline
                            joeQJ Offline
                            joeQ
                            wrote on last edited by
                            #13

                            @jsulm oh, Thanks and regards. I will try smart pointers in C++. And, I never used goto in university. and my teacher tell us, goto is bad style like you. but, when i join the software team. my leader is better than us. he is C programer. and, he let every body to use goto in program. now i am working.
                            Thank you every much.

                            Just do it!

                            1 Reply Last reply
                            0
                            • kshegunovK kshegunov

                              Avoid goto and ::exit() as the plague! The first one is a C remnant that dates back to asm's jmp instruction and has no place in good code, ever! The second one will allow destructors to be called only on some variables (i.e. global ones), everything else is left in a inconsistent state, so the less you use exit the better.

                              The "good" solution here, and in most cases, is to just give up as early as possible by returning the appropriate error code, e.g.:

                              int main(int argc, char ** argv)
                              {
                                  QApplication app(argc, argv);
                                  if (something_bad)
                                      return 1; // Calling ::exit will not allow QApplication to clean up
                              
                                  // ... more code
                                  return QApplication::exec();
                              }
                              
                              joeQJ Offline
                              joeQJ Offline
                              joeQ
                              wrote on last edited by
                              #14

                              @kshegunov @jsulm You said goto is bad style. But, the google's go language has the goto command. why didn't remove goto ? Every body said goto is bad. why didn't remove it. It is bad, but it si at here.

                              Just do it!

                              jsulmJ 1 Reply Last reply
                              0
                              • joeQJ joeQ

                                @kshegunov @jsulm You said goto is bad style. But, the google's go language has the goto command. why didn't remove goto ? Every body said goto is bad. why didn't remove it. It is bad, but it si at here.

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

                                @joeQ Because in very rare cases it can be useful. Another reason: code generators - it is much easier to write a code generator which uses goto, and generated code is usually not read that often, so goto disadvantages are not a big issue.

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

                                joeQJ 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @joeQ Because in very rare cases it can be useful. Another reason: code generators - it is much easier to write a code generator which uses goto, and generated code is usually not read that often, so goto disadvantages are not a big issue.

                                  joeQJ Offline
                                  joeQJ Offline
                                  joeQ
                                  wrote on last edited by
                                  #16

                                  @jsulm Thanks and regards. I get it. ^_^

                                  Just do it!

                                  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