将命令行参数传递给QApplication [英] passing command line arguments to QApplication

查看:389
本文介绍了将命令行参数传递给QApplication的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Qt应用程序,我必须传递一些命令行参数才能使其执行。在开始时用户需要键入FileName。我需要在文件目录中查找此文件。

这是我的不完整代码。

I have a Qt application for which i have to pass some command line arguements to make it execute. In the start user need to Type the FileName. Than i need to look for this file in file directory.
This is my incomplete code.

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QTextStream stream(stdin); //enter the file name
    QString UserFileName = stream.readLine();
// is it the correct method to get the file name from user in command line??
// if yes than how to add this value to argv[]??
    QStringList args = app.arguments();
    if (args.count() != 2)
    {
        std::cerr << "argument required" << endl;
        //return 1;
    }
    
    WorkerThread WThreadObj;
    WThreadObj.StartFileAnalysis(args[1]);   //pass cmd 2nd argument
    return app.exec();
}







感谢您阅读




Thanks for reading the question

推荐答案

参数传递给main函数,其中 argc 指定参数的数量, argv 是参数列表。第一个参数 argv [0] 是命令本身的名称,因此 argc 将至少为1. br />


这些参数传递给 QCoreApplication 构造函数,该构造函数初始化内部 QStringList 带参数。



您可以使用 argv (<$ c $)访问参数c> char * 使用特定于平台的编码)或 QApplication :: arguments() QStringList Unicode)。



因此无需在代码中询问文件名。但是,如果不存在必需的参数,则可以这样做。
The arguments are passed to the main function where argc specifies the number of arguments and argv is the list of arguments. The first argument argv[0] is the name of the command itself so that argc will be at least 1.

These parameters are passed to the QCoreApplication constructor that initialises an internal QStringList with the arguments.

You can access the arguments by using argv (char * with platform specific encoding) or QApplication::arguments() (QStringList Unicode).

So there is no need to ask for the file name as in your code. However, you might do so if a required argument was not present.


这篇关于将命令行参数传递给QApplication的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆