将命令行参数传递给QApplication [英] passing command line arguments to QApplication
本文介绍了将命令行参数传递给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 whereargc
specifies the number of arguments andargv
is the list of arguments. The first argumentargv[0]
is the name of the command itself so thatargc
will be at least 1.
These parameters are passed to theQCoreApplication
constructor that initialises an internalQStringList
with the arguments.
You can access the arguments by usingargv
(char *
with platform specific encoding) orQApplication::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屋!
查看全文