[英]Using QProcess with only arguments
I am trying to compile another.cpp code with QProcess.我正在尝试使用 QProcess 编译另一个.cpp 代码。 I use this code in my program.我在我的程序中使用此代码。
QStringList arguments;
arguments << "g++";
arguments << "-o";
arguments << QDir::toNativeSeparators(exePath); //path and exe path to future .exe
arguments << QDir::toNativeSeparators(mainPath); // path to main.cpp
QProcess process;
process.open();
process.setArguments(arguments);
process.start();
process.waitForFinished();
process.close();
It works and it compile another.cpp file and create.exe fot that tested project.它可以工作,它编译另一个.cpp 文件并为测试项目创建.exe。 But I also get warning:但我也收到警告:
QProcess::start: program not set QProcess::start: 程序未设置
But I dont want to set program...In the beginning I was using std::system() for this but I would like to use Qt libraries.但我不想设置程序...一开始我为此使用 std::system() 但我想使用 Qt 库。
Do you know how to surpass warning?你知道如何超越警告吗?
And again: This is program which is compiling totally different main.cpp from totally different program.再说一遍:这是从完全不同的程序编译完全不同的 main.cpp 的程序。
I dont want to set program我不想设置程序
as I can see you want to use g++
and that one is the program正如我所看到的,您想使用g++
并且那个是程序
so所以
QStringList arguments;
arguments << "-o";
arguments << QDir::toNativeSeparators(exePath); //path and exe path to future .exe
arguments << QDir::toNativeSeparators(mainPath); // path to main.cpp
QProcess process;
process.start("g++", arguments);
process.waitForFinished();
or或者
QStringList arguments;
arguments << "-o";
arguments << QDir::toNativeSeparators(exePath); //path and exe path to future .exe
arguments << QDir::toNativeSeparators(mainPath); // path to main.cpp
QProcess process;
process.setProgram("g++");
process.setArguments(arguments);
process.start();
process.waitForFinished();
Do you know how to surpass warning?你知道如何超越警告吗?
Use the setProgram()
function:使用setProgram()
function:
QProcess p;
//set the program
p.setProgram("g++");
//create args
QStringList arguments = {
"-o",
QDir::toNativeSeparators(exePath),
QDir::toNativeSeparators(mainPath)
};
//set the args
p.setArguments(arguments);
Additionally, don't use both open()
and start()
.此外,不要同时使用open()
和start()
。 From the docs :从文档:
This method is an alias for start(), and exists only to fully implement the interface defined by QIODevice.该方法是 start() 的别名,仅用于完全实现 QIODevice 定义的接口。
So just process.start()
is enough.所以只需process.start()
就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.