Interrupt startup / abort programm start in main()



  • 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?



  • @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;
    }
    

  • Moderators

    @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);
        }
    

  • Moderators

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



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



  • @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.


  • Moderators

    @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.



  • @joeQ @jsulm thank you both for your answer.

    Can be marked as solved!

    Regards
    Dominic



  • @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.


  • Moderators

    @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.


  • Moderators

    @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.


  • Qt Champions 2016

    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();
    }
    


  • @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.



  • @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.


  • Moderators

    @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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.