简体   繁体   English

如何使用QProcess从特定文件夹执行“ qmake -v> 1.txt”之类的命令?

[英]How to execute command like “qmake -v > 1.txt” from a specific folder with QProcess?

I have a path to a folder and a command to execute from that folder. 我有一个文件夹路径和一个从该文件夹执行的命令。 It's an arbitrary complex command. 这是一个任意复杂的命令。 For ex.: qmake -v > 1.txt . 例如: qmake -v > 1.txt

I figure I need to run a shell (cmd.exe because I'm on Windows atm.), and execute the command there: 我认为我需要运行一个shell(cmd.exe,因为我在Windows atm上),然后在其中执行命令:

QProcess::startDetached("cmd.exe", QString("qmake -v > 1.txt").split(' ', 

QString::SkipEmptyParts), "C:/folder/"); QString :: SkipEmptyParts),“ C:/ folder /”);

But it doesn't work. 但这是行不通的。 It does launch a console window (cmd.exe) at the specified path, but doesn't execute the command. 它会在指定路径上启动控制台窗口(cmd.exe),但不执行命令。 Then I've tried: 然后我尝试了:

QProcess::startDetached("cmd.exe", QStringList() << "start" << QString("qmake -v > 1.txt").split(' ', QString::SkipEmptyParts), "C:/folder/");

Also no luck. 也没有运气。 And finally, just to test if I'm going anywhere with this at all: 最后,只是测试一下我是否要去任何地方:

QProcess::startDetached("qmake.exe", QString("-r -tp vc").split(' ', QString::SkipEmptyParts), "C:/folder_containing_qt_project/");

A console window appears for a brief moment, but project is not generated. 会短暂显示一个控制台窗口,但不会生成项目。

What am I doing wrong, and how can I achieve what I want with QProcess (I don't mind using WinAPI if there's no other way, but strongly prefer to use the same code on Linux / Mac OS X / Windows)? 我在做什么错,如何使用QProcess实现我想要的功能(如果没有其他方法,我不介意使用WinAPI,但强烈希望在Linux / Mac OS X / Windows上使用相同的代码)?

It might be worth noting that in another method of the same app I have no problem executing notepad.exe <path to a text file> with startDetached . 可能值得注意的是,在同一应用程序的另一种方法中,我没有问题用startDetached执行notepad.exe <path to a text file>

I think you are looking for this method: 我认为您正在寻找这种方法:

void QProcess::setStandardOutputFile(const QString & fileName, OpenMode mode = Truncate) QProcess :: setStandardOutputFile(const QString&fileName,OpenMode mode = Truncate)

You would be using it then like this: 您将这样使用它:

QProcess myProcess;
myProcess.setStandardOutputFile("1.txt");
myProcess.start("qmake -v");
myProcess.waitForFinished();

You could also read the output into the application and write that out with QFile. 您也可以将输出读到应用程序中,并使用QFile将其写出。 This may get you going into that direction: 这可能会让您朝那个方向前进:

QByteArray QProcess::readAllStandardOutput() QByteArray QProcess :: readAllStandardOutput()

Regardless of the current read channel, this function returns all data available from the standard output of the process as a QByteArray. 不管当前的读取通道如何,此函数都以QByteArray的形式返回来自进程标准输出的所有可用数据。

Therefore, you could write something like this, but it is a bit more complicated, and potentially memory-hungry, so it is just for demonstratation purposes without chunk read and write. 因此,您可以编写类似这样的内容,但是它有点复杂,并且可能会占用大量内存,因此它仅用于演示目的,而无需读取和写入块。

QProcess myProcess;
myProcess.start("qmake -v");
myProcess.waitForFinished();
QFile file("1.txt");
file.open(QIODevice::WriteOnly);
file.write(myProcess.readAllStandardOutput());
file.close();

Disclaimer: I intentionally did not write error checking to keep it simple. 免责声明:为了简化起见,我故意没有编写错误检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM