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. QCommandLineParser to select gui or non-gui mode
Forum Updated to NodeBB v4.3 + New Features

QCommandLineParser to select gui or non-gui mode

Scheduled Pinned Locked Moved General and Desktop
qcommandlineparqcoreapplicatioqapplication
12 Posts 3 Posters 7.3k Views 3 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.
  • M Offline
    M Offline
    mcosta
    wrote on 15 Apr 2015, 18:00 last edited by
    #2

    Hi,

    in my project we use this techinque

    QSharedPointer<QCoreApplication> app;
    if(commandLine) {
        app.reset(new QCoreApplication(argc, argv));
    } else {
        app.reset(new QApplication(argc, argv));
    }
    

    where commandLine is initialized parsing the command line arguments

    Once your problem is solved don't forget to:

    • Mark the thread as SOLVED using the Topic Tool menu
    • Vote up the answer(s) that helped you to solve the issue

    You can embed images using (http://imgur.com/) or (http://postimage.org/)

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 15 Apr 2015, 18:11 last edited by
      #3

      You could also copy the contents of argv[] into a QStringList and then use QCommandLineParser before you create any Q*Application with:
      void QCommandLineParser::​process(const QStringList & arguments)

      M S 2 Replies Last reply 15 Apr 2015, 18:22
      1
      • ? A Former User
        15 Apr 2015, 18:11

        You could also copy the contents of argv[] into a QStringList and then use QCommandLineParser before you create any Q*Application with:
        void QCommandLineParser::​process(const QStringList & arguments)

        M Offline
        M Offline
        mcosta
        wrote on 15 Apr 2015, 18:22 last edited by
        #4

        @Wieland said:

        You could also copy the contents of argv[] into a QStringList and then use QCommandLineParser before you create any Q*Application with:
        void QCommandLineParser::​process(const QStringList & arguments)

        We use QCommandLineParser to parse the arguments

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        ? S 2 Replies Last reply 15 Apr 2015, 18:34
        0
        • ? A Former User
          15 Apr 2015, 18:11

          You could also copy the contents of argv[] into a QStringList and then use QCommandLineParser before you create any Q*Application with:
          void QCommandLineParser::​process(const QStringList & arguments)

          S Offline
          S Offline
          shavera
          wrote on 15 Apr 2015, 18:28 last edited by
          #5

          @Wieland
          This works... to a degree. 1, it still feels kludgey to construct a stringlist of arguments to parse.
          Second, I can't add in help or version options in this way. I have to instantiate the application to get these, I think?

          1 Reply Last reply
          0
          • M mcosta
            15 Apr 2015, 18:22

            @Wieland said:

            You could also copy the contents of argv[] into a QStringList and then use QCommandLineParser before you create any Q*Application with:
            void QCommandLineParser::​process(const QStringList & arguments)

            We use QCommandLineParser to parse the arguments

            ? Offline
            ? Offline
            A Former User
            wrote on 15 Apr 2015, 18:34 last edited by
            #6

            @mcosta
            I think @shavera's biggest concern is, like she said:

            If I want to use QCommandLineParser, I must first create an application object.

            Her assumption is wrong as there isn't only void QCommandLineParser::​process(const QCoreApplication & app) but also void QCommandLineParser::​process(const QStringList & arguments).

            @shavera said

            This works... to a degree. 1, it still feels kludgey to construct a stringlist of arguments to parse.
            Second, I can't add in help or version options in this way. I have to instantiate the application to get these, I think?

            This is all true. Maybe you should file a bug and request some improvement of QCommandLineParser. But honestly I don't think this would be implemented in the near future.

            S 1 Reply Last reply 15 Apr 2015, 18:39
            1
            • M mcosta
              15 Apr 2015, 18:22

              @Wieland said:

              You could also copy the contents of argv[] into a QStringList and then use QCommandLineParser before you create any Q*Application with:
              void QCommandLineParser::​process(const QStringList & arguments)

              We use QCommandLineParser to parse the arguments

              S Offline
              S Offline
              shavera
              wrote on 15 Apr 2015, 18:36 last edited by
              #7

              @mcosta I don't precisely understand what you mean in either comment. Using QCommandLineParser seems to either require a Q*Application already created, or to pass in a self-generated list of arguments to the parser, which one could then use to decide which type of application to generate. (but at the loss of some other functionality of QCommandLineParser, it seems to me)

              1 Reply Last reply
              0
              • ? A Former User
                15 Apr 2015, 18:34

                @mcosta
                I think @shavera's biggest concern is, like she said:

                If I want to use QCommandLineParser, I must first create an application object.

                Her assumption is wrong as there isn't only void QCommandLineParser::​process(const QCoreApplication & app) but also void QCommandLineParser::​process(const QStringList & arguments).

                @shavera said

                This works... to a degree. 1, it still feels kludgey to construct a stringlist of arguments to parse.
                Second, I can't add in help or version options in this way. I have to instantiate the application to get these, I think?

                This is all true. Maybe you should file a bug and request some improvement of QCommandLineParser. But honestly I don't think this would be implemented in the near future.

                S Offline
                S Offline
                shavera
                wrote on 15 Apr 2015, 18:39 last edited by
                #8

                @Wieland

                Yeah I was initially thinking along the lines of a bug report/suggested feature, but I wanted to make sure I wasn't overlooking some obvious solution. For instance, my initial attempt, I just used QCoreApplication::arguments() to generate the stringlist, forgetting that isn't a static function, which generated an error.

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on 15 Apr 2015, 18:40 last edited by A Former User
                  #9

                  @shavera Looks like there is no nice solution by now. I've actually seen multiple people complaining about this. :-(

                  1 Reply Last reply
                  1
                  • M Offline
                    M Offline
                    mcosta
                    wrote on 15 Apr 2015, 18:41 last edited by
                    #10

                    Hi,

                    the complete code is this

                        CommandFactory factory;
                        bool foundParams = factory.commandLineParamsArePresent(argc, argv, error);
                        
                        QSharedPointer<QCoreApplication> app;
                        if(foundParams) {
                            if(!error.isEmpty()) {
                                qDebug() << error;
                                return 1;
                            }
                            app = QSharedPointer<QCoreApplication>(new QCoreApplication(argc, argv));
                        } else {
                            app = QSharedPointer<QCoreApplication>(new CWinAdminApp(argc, argv));
                        }
                    

                    The CommandLineFactory::commandLineParamsArePresent() use a QCommandLineParser to parse the argv[] contents before creating any Q*Application instance.

                    Once your problem is solved don't forget to:

                    • Mark the thread as SOLVED using the Topic Tool menu
                    • Vote up the answer(s) that helped you to solve the issue

                    You can embed images using (http://imgur.com/) or (http://postimage.org/)

                    S 1 Reply Last reply 15 Apr 2015, 18:44
                    0
                    • M mcosta
                      15 Apr 2015, 18:41

                      Hi,

                      the complete code is this

                          CommandFactory factory;
                          bool foundParams = factory.commandLineParamsArePresent(argc, argv, error);
                          
                          QSharedPointer<QCoreApplication> app;
                          if(foundParams) {
                              if(!error.isEmpty()) {
                                  qDebug() << error;
                                  return 1;
                              }
                              app = QSharedPointer<QCoreApplication>(new QCoreApplication(argc, argv));
                          } else {
                              app = QSharedPointer<QCoreApplication>(new CWinAdminApp(argc, argv));
                          }
                      

                      The CommandLineFactory::commandLineParamsArePresent() use a QCommandLineParser to parse the argv[] contents before creating any Q*Application instance.

                      S Offline
                      S Offline
                      shavera
                      wrote on 15 Apr 2015, 18:44 last edited by
                      #11

                      @mcosta I can see how that could work, but it runs into the initial kludge problem, that you have to parse for whether a command line option is present (or more specifically, that a specific command line option is present in the case of multiple options), which you then use to construct the application (and potentially pass this application object through the CommandLineOption stuff itself)

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mcosta
                        wrote on 15 Apr 2015, 18:54 last edited by mcosta
                        #12

                        @shavera I understand what do you mean but honestly I don't think such kind of feature could be useful; you need anyway to do something manually to run in GUI or non-GUI mode.

                        Once your problem is solved don't forget to:

                        • Mark the thread as SOLVED using the Topic Tool menu
                        • Vote up the answer(s) that helped you to solve the issue

                        You can embed images using (http://imgur.com/) or (http://postimage.org/)

                        1 Reply Last reply
                        0

                        11/12

                        15 Apr 2015, 18:44

                        • Login

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