Using QApplication with command line arguments

≡放荡痞女 提交于 2019-12-20 03:51:05

问题


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

回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7514299/using-qapplication-with-command-line-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!