How to pass command line arguments from main.cpp to mainwindow.cpp



  • Hi All,
    I am passing two arguments when running my project from terminal in linux like
    ./project_exe <arg1> <arg2>
    These two values comes in main.cpp . Now i want to pass the values of these two arguments into a function defined in mainwindow.cpp.

    How this can be achieved?

    Kindly suggest...

    Regards,
    Anuj


  • Lifetime Qt Champion

    Hi,

    QCommandLineParser for the first part and then just call your MainWindow object function with the corresponding parameters.


  • Qt Champions 2017

    Hi
    what part is causing you an issue ?

    for only 2 arguments u can use plain c++

    int main(int argc, char* argv[]) {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();
    // dump them :)
      for (int i = 1; i < argc; i++) {
        qDebug() << "param" << i << " is " << argv[i];
      }
    
      if (argc > 2 ) { 
        QString name(argv[1]);
        int id = atoi(argv[2]);
        qDebug() << "call func with" << id << " and " << name;
        w.MyLovelyFunction(id, name); // call some function in w mainwinow
      }
    

    in this test i call it like
    app.exe NAME 40
    so string and int.



  • Thanks for reply... it worked for me...


Log in to reply
 

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