[SOLVED] How to use main args



  • Hi,

    I'm making a Qt Gui application so the main its like:

    @
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv); //Maybe I can do something with that?
    MainWindow w;
    w.show();

    return a.exec();
    

    }
    @

    I need to use those arguments (argc, argv[]) in the mainwindow. So my question is: How can I use them there? The only solution I can think of is to create a new class which receives them, put them in global variables and then inherite mainwindow from the new class but I don't really like it and I suposse there is an easer way to do it.
    I've been searching the QApplication behaviour but nothing found

    Thanks!


  • Lifetime Qt Champion

    Hi,

    You are looking for "this":http://doc.qt.io/qt-5/qcoreapplication.html#accessing-command-line-arguments part of the documentation.



  • Yeah! That's it!

    @QStringList ArgsList = QApplication::arguments(); @

    Next time I'll say: Hi Sgaist I think XDDD Thank you for all :D



  • [quote author="roseicollis" date="1419334108"]Hi,

    I'm making a Qt Gui application so the main its like:

    @
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv); //Maybe I can do something with that?
    MainWindow w;
    w.show();

    return a.exec();
    

    }
    @

    I need to use those arguments (argc, argv[]) in the mainwindow. So my question is: How can I use them there? The only solution I can think of is to create a new class which receives them, put them in global variables and then inherite mainwindow from the new class but I don't really like it and I suposse there is an easer way to do it.
    I've been searching the QApplication behaviour but nothing found

    Thanks![/quote]

    You can do this

    @
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv); //Maybe I can do something with that?
    MainWindow w(argc, argv); // And use it in your constructor of MainW
    w.show();

    return a.exec();
    

    }
    @



  • Hi Francknos,

    Sorry but if I do that ( I already tried it before psoting here), you can't use them in mainwindow as int a = argc; or something like that.

    Anyway as the title says, it has been already solved so thanks :)



  • Ok no problem.
    But why you want modify the argc, argv in your app ?



  • I didnt want to modify them, just to use them :)


Log in to reply
 

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