运行资源文件时重定向 QProcess 的输出

Sph*_*ics 2 c++ qt qprocess qresource

对于 Qt 来说相当新。

我正在使用 QProcess 运行外部 shell 脚本并将输出重定向到 GUI 上的文本浏览器。代码:

在mainwindow.h中:

private:
   QProcess *myProcess;
Run Code Online (Sandbox Code Playgroud)

和主窗口.cpp:

void MainWindow::onButtonPressed(){
   myProcess = new QProcess(this);
   myProcess->connect(myProcess, SIGNAL(readyRead()), this, SLOT(textAppend()));
   myProcess->start("./someScript.sh", arguments);
}

void MainWindow::textAppend(){
   ui->textBrowser->append(myProcess->readAll());
}
Run Code Online (Sandbox Code Playgroud)

这非常适合运行外部脚本。我的问题是如何对作为资源文件包含的脚本应用相同的过程。我尝试简单地替换"./someScript.sh"为资源版本":/someScript.sh",但它似乎不起作用。资源脚本运行完美,但控制台输出消失。

小智 6

因此,有一个叫做“ QTemporaryFile ”的类。

\n\n

因为您需要调用系统中已存在的文件- 好吧!

\n\n

让我们看这个例子:

\n\n

使用QProcess我们需要从资源运行一个 python 文件

\n\n
//[1] Get Python File From Resource\nQFile RsFile(":/send.py");\n//[2] Create a Temporary File\nQTemporaryFile *NewTempFile = QTemporaryFile::createNativeFile(RsFile);\n//[3] Get The Path of Temporary File\nQStringList arg;\narg << NewTempFile->fileName();\n//[4] Call Process\nQProcess *myProcess = new QProcess(this);\nmyProcess->start("python", arg);\n//[5] When You Finish, remove the temporary file\nNewTempFile->remove();\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

注意:在 Windows 上,临时文件存储在%TEMP%目录中

\n
\n\n

有关更多信息,您可以访问Qt 文档 - QTemporaryFile 类

\n\n

祝你好运\xe2\x99\xa5

\n