Reputation: 33579
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
.
I figure I need to run a shell (cmd.exe because I'm on Windows atm.), and execute the command there:
QProcess::startDetached("cmd.exe", QString("qmake -v > 1.txt").split(' ',
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. 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)?
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
.
Upvotes: 1
Views: 301
Reputation: 53173
I think you are looking for this method:
void 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. This may get you going into that direction:
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.
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.
Upvotes: 1