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. How to create a console application with command line arguments in Qt?
Forum Updated to NodeBB v4.3 + New Features

How to create a console application with command line arguments in Qt?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 5 Posters 4.5k 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.
  • G Offline
    G Offline
    Guerrian
    wrote on 28 Jan 2020, 13:39 last edited by
    #1

    I have looked at this online and seen various approaches such as using QApplication or QCoreApplication. I would like to use Qt objects for the application and parse the command line. For example to parse a command line such as:

    myapp -foo bar -qaz 10
    

    Which approach is best?

    Linux Mint 18.3
    Qt 5.14.1
    Qt Creator 4.11.1

    J J O P 4 Replies Last reply 28 Jan 2020, 13:43
    0
    • G Guerrian
      28 Jan 2020, 13:39

      I have looked at this online and seen various approaches such as using QApplication or QCoreApplication. I would like to use Qt objects for the application and parse the command line. For example to parse a command line such as:

      myapp -foo bar -qaz 10
      

      Which approach is best?

      J Offline
      J Offline
      JonB
      wrote on 28 Jan 2020, 13:43 last edited by JonB
      #2

      @Guerrian
      You can pick whichever kind of QApplication is best for your program, and you can access command-line arguments either via QCoreApplication::arguments() (if you don't need a GUI) or other Qt classes, or from the C++ main(argc, argv) as you please.

      1 Reply Last reply
      3
      • G Guerrian
        28 Jan 2020, 13:39

        I have looked at this online and seen various approaches such as using QApplication or QCoreApplication. I would like to use Qt objects for the application and parse the command line. For example to parse a command line such as:

        myapp -foo bar -qaz 10
        

        Which approach is best?

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 28 Jan 2020, 13:44 last edited by
        #3

        @Guerrian
        Qt has for this a adequately named class QCommandLineParser

        Might be helpful 😉


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        8
        • G Guerrian
          28 Jan 2020, 13:39

          I have looked at this online and seen various approaches such as using QApplication or QCoreApplication. I would like to use Qt objects for the application and parse the command line. For example to parse a command line such as:

          myapp -foo bar -qaz 10
          

          Which approach is best?

          O Offline
          O Offline
          ODБOï
          wrote on 28 Jan 2020, 13:45 last edited by
          #4

          @Guerrian hi

          for (int i = 1; i < argc; i++) {
                 qDebug() << "param" << i << " is " << argv[i];
               }
          
          1 Reply Last reply
          2
          • G Guerrian
            28 Jan 2020, 13:39

            I have looked at this online and seen various approaches such as using QApplication or QCoreApplication. I would like to use Qt objects for the application and parse the command line. For example to parse a command line such as:

            myapp -foo bar -qaz 10
            

            Which approach is best?

            P Offline
            P Offline
            Pl45m4
            wrote on 28 Jan 2020, 13:48 last edited by Pl45m4
            #5

            @Guerrian

            Hi,

            there is a QCommandLineParser-class. (https://doc.qt.io/qt-5/qcommandlineparser.html#details)
            Have a look at this.

            Edit:
            Damn, I'm typing too slow... :) @J-Hilk


            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
            4

            1/5

            28 Jan 2020, 13:39

            • Login

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