Using QApplication with command line arguments

后端 未结 3 503
臣服心动
臣服心动 2021-01-23 05:54
QApplication::QApplication ( int & argc, char ** argv )

Initializes the window system and constructs an application object

相关标签:
3条回答
  • 2021-01-23 06:41

    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.

    0 讨论(0)
  • 2021-01-23 06:44

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

    0 讨论(0)
  • 2021-01-23 06:47

    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.

    0 讨论(0)
提交回复
热议问题