运行 资源文件时重定向 QProcess 的输出
Redirecting the output of QProcess when running a resource file
Qt 的新手。
我正在使用 QProcess 运行 外部 shell 脚本并将输出重定向到我的 GUI 上的 textBrowser。代码:
在mainwindow.h中:
private:
QProcess *myProcess;
和mainwindow.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());
}
这对 运行 外部脚本非常有效。我的问题是如何将相同的过程与作为资源文件包含的脚本一起应用。
我试过简单地用资源版本 ":/someScript.sh"
替换 "./someScript.sh"
但它似乎不起作用。
资源脚本 运行 完美,但控制台输出消失。
我不工作,因为当你 运行 myProcess->start(":/someScript.sh", arguments);
你要求你的系统 运行 :/someScript.sh
这对你的系统不存在。
一个快速的解决方案是将脚本复制到一个临时文件夹并从那里运行它。
QFile::copy(":/someScript.sh", pathToTmpFile);
myProcess->start(pathToTmpFile, arguments);
我还建议您使用 QTemporaryFile
来获得一个唯一的临时文件名。
为此,有一个叫做“QTemporaryFile”的东西 class.
因为您需要调用系统中已经存在的文件 - 好的!
让我们举个例子:
使用 QProcess 我们需要 运行 来自资源 python 的文件
//[1] Get Python File From Resource
QFile RsFile(":/send.py");
//[2] Create a Temporary File
QTemporaryFile *NewTempFile = QTemporaryFile::createNativeFile(RsFile);
//[3] Get The Path of Temporary File
QStringList arg;
arg << NewTempFile->fileName();
//[4] Call Process
QProcess *myProcess = new QProcess(this);
myProcess->start("python", arg);
//[5] When You Finish, remove the temporary file
NewTempFile->remove();
Note : on windows, the Temporary File stored in %TEMP% directory
有关更多信息,您可以访问 Qt Documentation - QTemporaryFile Class
祝你好运♥
Qt 的新手。
我正在使用 QProcess 运行 外部 shell 脚本并将输出重定向到我的 GUI 上的 textBrowser。代码:
在mainwindow.h中:
private:
QProcess *myProcess;
和mainwindow.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());
}
这对 运行 外部脚本非常有效。我的问题是如何将相同的过程与作为资源文件包含的脚本一起应用。
我试过简单地用资源版本 ":/someScript.sh"
替换 "./someScript.sh"
但它似乎不起作用。
资源脚本 运行 完美,但控制台输出消失。
我不工作,因为当你 运行 myProcess->start(":/someScript.sh", arguments);
你要求你的系统 运行 :/someScript.sh
这对你的系统不存在。
一个快速的解决方案是将脚本复制到一个临时文件夹并从那里运行它。
QFile::copy(":/someScript.sh", pathToTmpFile);
myProcess->start(pathToTmpFile, arguments);
我还建议您使用 QTemporaryFile
来获得一个唯一的临时文件名。
为此,有一个叫做“QTemporaryFile”的东西 class.
因为您需要调用系统中已经存在的文件 - 好的!
让我们举个例子:
使用 QProcess 我们需要 运行 来自资源 python 的文件
//[1] Get Python File From Resource
QFile RsFile(":/send.py");
//[2] Create a Temporary File
QTemporaryFile *NewTempFile = QTemporaryFile::createNativeFile(RsFile);
//[3] Get The Path of Temporary File
QStringList arg;
arg << NewTempFile->fileName();
//[4] Call Process
QProcess *myProcess = new QProcess(this);
myProcess->start("python", arg);
//[5] When You Finish, remove the temporary file
NewTempFile->remove();
Note : on windows, the Temporary File stored in %TEMP% directory
有关更多信息,您可以访问 Qt Documentation - QTemporaryFile Class
祝你好运♥