2
QApplication::QApplication ( int & argc, char ** argv )

Initializes the window system and constructs an application object with argc command line arguments in argv.

Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QApplication object. In addition, argc must be greater than zero and argv must contain at least one valid character string.

From this link: http://doc.qt.io/qt-4.8/qapplication.html#QApplication

What can be the arguments to the executable file? Any examples?

I tried specifying something like:

anisha@linux-dopx:~/Desktop/notes/qt> make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../qtsdk-2010.05/qt/mkspecs/linux-g++-64 -I. -I../../../qtsdk-2010.05/qt/include/QtCore -I../../../qtsdk-2010.05/qt/include/QtGui -I../../../qtsdk-2010.05/qt/include -I. -I. -o widgets.o widgets.cpp
g++ -m64 -Wl,-O1 -Wl,-rpath,/home/anisha/qtsdk-2010.05/qt/lib -o qt widgets.o    -L/home/anisha/qtsdk-2010.05/qt/lib -lQtGui -L/home/anisha/qtsdk-2010.05/qt/lib -L/usr/X11R6/lib64 -lQtCore -lpthread 

anisha@linux-dopx:~/Desktop/notes/qt> ./qt 2 f g
anisha@linux-dopx:~/Desktop/notes/qt> 

Nothing special happened, nor I knew what I was doing or what I was supposed to do.

EDIT 1: The code on which I tried the ./qt -style=windows.

#include <QtGui>

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

    QWidget objQWidget;
    objQWidget.show                 ();     
    objQWidget.resize               (320, 240);     
    objQWidget.setWindowTitle ("Text to be shown on the title bar\n");

    // Adding a "child" widget.
    QPushButton *objQPushButton = new QPushButton ("Text to be shown on the button", &objQWidget);
    objQPushButton->move         (100, 100);
    objQPushButton->show         ();

    return app.exec                   ();
 }
Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

4 Answers4

6

The arguments passed in the constructor are later accessible through the static method
QStringList QCoreApplication::arguments(). By this, command line arguments can be handled everywhere in your code.

Karmic Coder
  • 17,569
  • 6
  • 32
  • 42
king_nak
  • 11,313
  • 33
  • 58
5

Continue reading that documentation. The set of flags QApplication acts on is listed there.

Try for example:

./qt -style=windows

The arguments that QApplication doesn't deal with are simply left alone. The ones it does process are removed (which is why that function takes non-const arguments).

Mat
  • 202,337
  • 40
  • 393
  • 406
  • Oh, yeah, thanks, I did read it further now, saw a lot of flags, tried your example too, but it didn't do anything special to the output. Well anyway, that's another problem. – Aquarius_Girl Sep 22 '11 at 12:11
  • I must make a point by now, to at least read the full page before asking the question here. – Aquarius_Girl Sep 22 '11 at 12:12
  • It won't do anything if you don't display a GUI... And since you didn't post the code you used above, I have no idea if you're passing `main`'s arguments to `QApplication`'s constructor. – Mat Sep 22 '11 at 12:13
  • 1
    Well try the other styles. Your code is fine (except that aligning the arguments like that is really uncommon and, to me, really distracting). Print out `argc` before and after having initialized QApplication to see that that constructor is "eating" some of the arguments. – Mat Sep 22 '11 at 12:24
4

The suggestion about using QCoreApplication is only recommended of you have a console application. If you are using a QApplication instead, and want to access command-line arguments from inside a QWidget, you can do it with the global pointer qApp:

Here you can find the documentation from Nokia, or here from qt-project.org . In the documentation browser of Qt Creator I couldn't find it, so it is at best not that easily accessible.

so you can find:

int my_argc = qApp->arguments().count();

QString my_argv_0 = qApp->arguments.at(0);

...

and so on.

I know this question is old, but took me some time to find a way to do it from within my Main Window, so hope this helps someone else.

1

Thanks, Dissident penguin! This helped me a lot! Just note that:

QString my_argv_0 = qApp->arguments.at(0);

should be replaced with:

QString my_argv_0 = qApp->arguments().at(0);

(note the additional () after 'arguments')

Koby.G
  • 73
  • 9