PDA

View Full Version : How to use main args



roseicollis
23rd December 2014, 11:14
Hi,

I'm amking 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 this line?
}

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.

Thanks!

Radek
23rd December 2014, 11:28
First, you always need int main( int argc, char *argv[] ) because you need to pass argc and argv to QApplication. Next (seeing a Linux programmer), there are no __argc and __argv in Linux so that you need to catch argc and argv yourself when they pass by. Therefore:

(1) Derive a class MyApplication from QApplication. Give MyApplication data items argc and argv.
(2) In the MyApplication ctor, copy argc and argv to your data items. Now:


int main( int argc, char *argv[] )
{
MyApplication app(argc, argv);

// etc...
}

roseicollis
23rd December 2014, 11:38
Hi!
Yes, I'm running it on unix. Thank you for the answer.
As I see the only way is to create an other class then.

And then what should I do? Include of MyApplication on mainwindow.cpp or inherit from it like class MainWindow : public QMainWindow, MyApplycation ?? I can't see neither options hehe

anda_skoa
23rd December 2014, 11:59
I need to use those arguments (argc, argv[]) in the mainwindow. So my question is: How can I use them there?
QCoreApplication::arguments(), QCommandLineParser.

Cheers,
_

roseicollis
23rd December 2014, 12:36
Yeah! That was anda_skoa, thank you!

I let here the line for those who visit that thread after :)

QStringList ArgsList = QApplication::arguments();