我的 Qt5 程序如何从控制台获取参数?
How my Qt5 program get arguments from the console?
我在 qt5 中创建了一个控制台程序,它应该在 运行ning 之前从控制台获取参数。
这是我使用从控制台传递的参数的代码部分:
void foo::start(){
if(arguments.contains(--help))
show help function
else if (arguments.contains(--ipinfo))
show ip info function
else if (arguments.contains(--time))
show time info function
else
nothing
}
我的程序名称是initlizer
。当我 运行 我的程序通过带有参数的控制台时,我想使用 qt5 从控制台获取参数。例如:
$initlizer --help >> show help function
$initlizer --time >> show time function
处理命令行参数的"Qt way"是将main的argc和argv传递给QCoreApplication构造函数,然后使用QCommandLineParser查询参数。 (link 包含大量示例代码。)
在 C++ 中,要将参数传递给控制台程序,您必须将参数添加到源代码的主函数中。主函数中的这些参数决定了他在执行时接收和使用的输入值。
For example:
This is an example of a non-parameters main function:
public int main ()
{
// Functions body.
}
This is an example of a main function that receive an string as input:
public int main (int argc, char * argv[])
{
// Functions body.
}
This is an example of a main function that receives multiple arguments as input:
public int main (int argc, char * argv[], // other parameters)
{
// Functions body.
}
要以 Qt 方式执行此操作,您必须在实例化 app
时在 QCoreApplication
构造函数中定义参数,然后使用 QCommandLineParser
获取由控制台。
请参阅本 page 的详细说明部分中的示例。
您可以从这个 page.
中获得有关 main 函数中参数的更多信息
我在 qt5 中创建了一个控制台程序,它应该在 运行ning 之前从控制台获取参数。
这是我使用从控制台传递的参数的代码部分:
void foo::start(){
if(arguments.contains(--help))
show help function
else if (arguments.contains(--ipinfo))
show ip info function
else if (arguments.contains(--time))
show time info function
else
nothing
}
我的程序名称是initlizer
。当我 运行 我的程序通过带有参数的控制台时,我想使用 qt5 从控制台获取参数。例如:
$initlizer --help >> show help function
$initlizer --time >> show time function
处理命令行参数的"Qt way"是将main的argc和argv传递给QCoreApplication构造函数,然后使用QCommandLineParser查询参数。 (link 包含大量示例代码。)
在 C++ 中,要将参数传递给控制台程序,您必须将参数添加到源代码的主函数中。主函数中的这些参数决定了他在执行时接收和使用的输入值。
For example:
This is an example of a non-parameters main function:
public int main () { // Functions body. }
This is an example of a main function that receive an string as input:
public int main (int argc, char * argv[]) { // Functions body. }
This is an example of a main function that receives multiple arguments as input:
public int main (int argc, char * argv[], // other parameters) { // Functions body. }
要以 Qt 方式执行此操作,您必须在实例化 app
时在 QCoreApplication
构造函数中定义参数,然后使用 QCommandLineParser
获取由控制台。
请参阅本 page 的详细说明部分中的示例。
您可以从这个 page.
中获得有关 main 函数中参数的更多信息