How to create command line arguments



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


  • Lifetime Qt Champion

    Hi,

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

    Hope it helps



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


  • Lifetime Qt Champion

    Didn't you forgot

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

    ?



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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion



  • 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;
    ~~~~~~~~^~@


  • Lifetime Qt Champion

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

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



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


Log in to reply
 

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