Search code examples
qtqt4

Using QApplication with command line arguments


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                   ();
 }

Solution

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