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 command line arguments
Forum Updated to NodeBB v4.3 + New Features

How to create command line arguments

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 3.8k Views 1 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.
  • R Offline
    R Offline
    rabcor
    wrote on last edited by
    #1

    I'm trying to make a relatively simple program for Linux, and I need to have one possible command line argument. Let's say the executable name/command is program, I want to be able to launch it with a command line parameter, m. So program -m and i will have it start with a second window open which will be closed by default if no command line parameter/argument was passed.

    How do I make the program look for and understand command line arguments?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      One possibility is "QCommandLineParser":http://doc.qt.io/qt-5/qcommandlineparser.html

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rabcor
        wrote on last edited by
        #3

        Yes that looks like exactly what I was looking for, but I can't seem to figure out how to use it.

        I added private: bool showMR; in mainwindow.h, then in main.cpp (main class)

        @ QApplication a(argc, argv);
        QCommandLineParser parser;
        QCommandLineOption showMROption("m", QCoreApplication::translate("main", "Shows the MR Buffer file on startup."));
        parser.addOption(showMROption);
        bool showMR = parser.isSet(showMROption);@

        and finally in mainwindow.cpp

        @ if (showMR)
        ui->MR_Buffer->setText("MR SHOW");@

        i went to run options and added to the argument line m (I also tried -m) but the test case I put in the if(showMR) never occurs ("MR SHOW" is not printed to MR_Buffer) where did I go wrong?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Didn't you forgot

          @
          // Process the actual command line arguments given by the user
          parser.process(app);
          @

          ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rabcor
            wrote on last edited by
            #5

            Yes I indeed did forget that one. I added it in but my results did not change.

            I think it has to do with my bool showMR. If I change my line in main.cpp from

            @bool showMR = parser.isSet(showMROption);@

            to

            @showMR = parser.isSet(showMROption);@

            it tells me Use of undeclared identifier 'showMR' (which means main.cpp, or at least the main class in it is unable to read/write the showMR bool from mainwindow.h, whereas mainwindow.cpp reads it just fine; but reads it as false since main.cpp doesn't seem to be able to interact with it. (And yes mainwindow.h is included in the main.cpp file)

            How do I pass the variable/argument/bool from main.cpp to mainwindow.cpp?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Add a setter to your MainWindow class where you update the value of showMR and proceed with the update of your UI

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rabcor
                wrote on last edited by
                #7

                I have never heard of setters, how do I do this?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  "Here":http://stackoverflow.com/questions/15383167/how-to-write-setter-for-class-member-variable you have an example

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    rabcor
                    wrote on last edited by
                    #9

                    I'm still running into problems with setting showMR... under public in mainwindow.h MainWindow class:

                    @ bool showMR;
                    bool m_showMR;
                    void setshowMR(bool showMR){m_showMR = showMR;}@

                    in main.cpp

                    @ MainWindow::showMR = parser.isSet(showMROption);@

                    Debugger:

                    @main.cpp:11: error: invalid use of non-static data member 'showMR'
                    MainWindow::showMR = true;
                    ~~~~~~~~^~@

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      showMR and m_showMR are not a static variables (nor should they be)

                      @
                      MainWindow mw;
                      mw.setShowMR(parser.isSet(showMROption));
                      @

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        rabcor
                        wrote on last edited by
                        #11

                        Allright, I saw some progress. (As in it actually compiled this time and ran through my if/switch)

                        mainwindow.h
                        @public:
                        bool showMR;
                        bool m_showMR;
                        void setshowMR(bool showMR) {m_showMR = showMR;}

                        private:
                        bool getshowMR() {return m_showMR;}@

                        main.cpp (main class)
                        @ QApplication a(argc, argv);
                        QCommandLineParser parser;
                        QCommandLineOption showMROption("m", QCoreApplication::translate("main", "Shows the MR Buffer file on startup."));
                        parser.addOption(showMROption);
                        parser.process(a);
                        MainWindow w;
                        w.setshowMR(parser.isSet(showMROption));
                        w.show();

                        return a.exec();@
                        

                        mainwindow.cpp
                        @ ui->setupUi(this);
                        loadFiles();
                        switch (getshowMR()){
                        case true:
                        ui->MR_Buffer->setText("MR SHOW");
                        break;
                        case false:
                        ui->MR_Buffer->setText("MR HIDE");
                        break;@

                        Right now the switch is always returning "false". If I do what (i think) you said

                        main.cpp
                        @ QApplication a(argc, argv);
                        QCommandLineParser parser;
                        QCommandLineOption showMROption("m", QCoreApplication::translate("main", "Shows the MR Buffer file on startup."));
                        parser.addOption(showMROption);
                        MainWindow mw;
                        parser.process(a);
                        mw.setshowMR(parser.isSet(showMROption));
                        MainWindow w;
                        w.show();

                        return a.exec();@
                        

                        The switch always returns "true" (I checked if it made any difference to add the "m" or "-m" or remove the argument in the project settings, it had no effect) but at least it's compiling by now.

                        What seems to be happening is that if I initialize two instances of the MainWindow class, the getter will return True, if I initialize only one instance the getter will return False, which is completely beyond my understanding, and of course is completely ignoring my command line arguments.

                        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