我需要强制我的c ++ QT4应用程序从linux命令读取结果.我正在尝试使用Qprocess但是一旦我的命令变得复杂,它就会以某种方式混乱(只是猜测)并且不起作用.
在这里,我尝试为yu做一个小例子:
QProcess process;
command = "ls -l | grep a | sort";
qDebug() << "Execute command -> "+command;
process.start( command );
process.waitForFinished(-1);
QString processStdout = process.readAllStandardOutput();
QString processStderr = process.readAllStandardError();
qDebug() << "Std out -> "+processStdout;
qDebug() << "Std err -> "+processStderr;
Run Code Online (Sandbox Code Playgroud)
这将打印:
Execute command -> ls -l | grep a | sort
"Std out -> "
"Std err -> ls: |: No such file or directory
Run Code Online (Sandbox Code Playgroud)
如果从consol中删除,将正确打印文件名.
如果我用更简单的方式替换command = "ls -l";comman , 那么它可以顺利工作.操作系统在标准错误中返回错误.
因此,我猜测用于命令的Qstring会以某种方式被操纵.关于wht发生的任何想法?
我想在我的Qt应用程序中按下按钮时启动可执行文件.它.exe始终与Qt应用程序本身位于同一目录中.
有时在此目录的路径中有空格.这似乎阻止了它.exe的启动.
这是我的代码(似乎不起作用):
QString path = QCoreApplication::applicationDirPath ();
path.append("/executable.exe");
QProcess process;
process.execute(path);
Run Code Online (Sandbox Code Playgroud)
我不知道是否可以先启动.exe而不显示命令提示符.当.exe正在运行时,我必须关闭Qt应用程序,同时.exe继续运行.
我在使用QProcess时遇到了麻烦,我已经查看了几个使用它的位置,但是每次我使用它时我的程序都会冻结,或者它只是没有做我想做的事情.
我想从GUI应用程序做的事情如下:
将目录更改为/ Users/Tim/etc等.从那里我需要调用gnuplot并将脚本加载到其中.
我通常会在终端窗口中执行以下操作:
> cd /Users/Tim/...
> /opt/local/bin/gnuplot barchartscript.txt
Run Code Online (Sandbox Code Playgroud)
目前我正在使用系统调用来执行此操作,这是有效的,但是每个人都建议使用QProcess,所以我想这样做.
我的代码现在如何使用QProcess:
QObject *parent;
QProcess *process = new QProcess(parent);
QString commands;
QString changed = "cd /Users/Tim/etcetc";
commands = (changed + "&& /opt/local/bin/gnuplot scatterplotscriptwithout.txt").c_str();
process->start(commands);
Run Code Online (Sandbox Code Playgroud)
谁能告诉我什么是错的?或者在一个进程中执行多个命令的正确方法?
我需要做的是
我正在构建一个运行exe的qt应用程序并将其输出传递给其他exe.(在我的情况下ffmpeg | x265)
我做了什么
QProcess ffmpeg;
QProcess x265;
ffmpeg.setStandardOutputProcess(&x265);
x265.setProcessChannelMode(QProcess::ForwardedChannels);
ffmpeg.start(ffmpegArgs);
x265.start(x265Args);
if(!ffmpeg.waitForStarted())
return;
bool retval = false;
while ((retval = x265.waitForFinished(-1)))
{}
ffmpeg.close();
x265.close();
Run Code Online (Sandbox Code Playgroud)
每件事情都很好,但是当流程运行时,GUI会冻结.
我试图解决这个问题
void Basic::on_btnEncode_clicked()
{
if(fileContainer -> getQueue() -> rowCount() == 0) {
QMessageBox msg;
msg.setText("No Input to Convert");
msg.setIcon(QMessageBox::Information);
msg.exec();
}
QString file;
int bitRate;
QString preset;
QString ffmpegArgs;
QString x265Args;
bitRate = ui->sldBitRate->value();
preset = mapPreset(ui->sldPreset->value());
for(int i = 0; i < fileContainer->getQueue()->rowCount(); ++i)
{
file = QString("\"") + fileContainer->getQueue()->item(i, …Run Code Online (Sandbox Code Playgroud) 我想制作一个小型Qt应用程序,以便能够使用-login登录密码作为启动选项快速切换蒸汽帐户.问题是我无法使用我的Qt应用程序运行steam.exe和所需的参数.这是一个代码:
QString path("C:\\Program Files (x86)\\Steam");
QStringList arg;
arg << "-launch 123 123";
QString item = arg.takeAt(0);
QString res("Steam.exe");
QString program = "\"" + path + "/" + res + "\"";
QProcess* process = new QProcess();
process->startDetached(program, arg);
Run Code Online (Sandbox Code Playgroud) 对于 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",但它似乎不起作用。资源脚本运行完美,但控制台输出消失。
我正在尝试使用队列运行多个进程并获取所有进程的输出QProcess,但我遇到了一些问题。我正在使用 aQSpinBox设置同时运行的最大进程,并且我可以让主线程中的一切正常工作,或者如果我使用 a 中的进程运行循环,QObject但我无法让它在 a 中正常工作QThread。
我知道不需要使用线程,QProcess但是使用循环我几乎别无选择。当在主线程中运行时,它会暂时冻结,直到进程启动,我宁愿让它运行得更顺畅。除非我使用类似的东西,否则
我只会在尝试运行进程时遇到错误,但问题是进程一次只运行一个。
有没有人有任何建议可以使其正常工作?我目前正在使用 Pyside2,但 Pyside2 或 PyQt5 的答案就可以了。谢谢。 QThread_process.waitForFinished()
import queue
import sys
from PySide2.QtCore import QProcess, QTextCodec, QThread, Qt
from PySide2.QtWidgets import QApplication, QWidget, QSpinBox, \
QPushButton, QVBoxLayout
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setAttribute(Qt.WA_DeleteOnClose, True)
self.queue = queue.Queue()
layout = QVBoxLayout(self)
self.startBtn = QPushButton('Start', clicked=self.addToQueue)
self.spinBox = QSpinBox(value=3)
layout.addWidget(self.spinBox)
layout.addWidget(self.startBtn)
self.taskList = ['my.exe -value','my.exe -value','my.exe -value','my.exe -value',
'my.exe -value','my.exe -value','my.exe -value','my.exe -value'] …Run Code Online (Sandbox Code Playgroud) 如何终止在QThread中运行并被另一个QThread删除的正在进行的QProcess?我甚至插入了一个QMutex extCmdProcessLock,它应该在extCmdProcess完成或超时之前避免破坏DbManager.如果另一个线程在DbManager上调用delete,我会在"waitForStarted"上遇到分段错误.我不能使用信号(我认为)因为我在顺序数据处理中使用外部命令.非常感谢您的帮助!
DbManager::extCmd(){
...
QMutexLocker locker(&extCmdProcessLock);
extCmdProcess = new QProcess(this);
QString argStr += " --p1=1"
+ " --p2=3";
extCmdProcess->start(cmd,argStr.split(QString(" ")));
bool startedSuccessfully = extCmdProcess->waitForStarted();
if (!startedSuccessfully) {
extCmdProcess->close();
extCmdProcess->kill();
extCmdProcess->waitForFinished();
delete extCmdProcess;
extCmdProcess = NULL;
return;
}
bool successfullyFinished = extCmdProcess->waitForFinished(-1);
if (!successfullyFinished) {
qDebug() << "finishing failed"; // Appendix C
extCmdProcess->close();
extCmdProcess->kill();
extCmdProcess->waitForFinished(-1);
delete extCmdProcess;
extCmdProcess = NULL;
return;
}
extCmdProcess->close();
delete extCmdProcess;
extCmdProcess = NULL;
}
DbManager::~DbManager(){
qDebug() << "DB DbManager destructor called.";
QMutexLocker locker(&extCmdProcessLock);
if (extCmdProcess!= NULL){ …Run Code Online (Sandbox Code Playgroud) 文档说error()如果子进程崩溃,将发出信号,但是也会finished()发出信号,或者它只在成功退出时发出?
我需要通过 QProcess 运行一个可见的 cmd.exe 窗口,并最终终止它。shell 的控制台窗口必须在前台,并且必须对用户可见。
如果我使用 启动它QProcess::start(),控制台窗口永远不会出现。如果我使用类方法启动它QProcess::startDetached(),我不能使用实例方法terminate()来终止它。
这是上一个问题的后续问题(我再次发布):PyQt4 QProcess 状态始终为 0,各种插槽也不起作用
代码(修改):
主要应用:qprocess_test.py
#!/usr/bin/python
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QProcess
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.command = "./testCommand.py"
self.args = [""]
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
qbtn = QtGui.QPushButton('Start', self)
qbtn.clicked.connect(self.toggleProcess)
qbtn.resize(qbtn.sizeHint())
hbox.addWidget(qbtn)
# This button is for testing the responsiveness of the GUI after the QProcess has been started
qbtn2 = QtGui.QPushButton('Click me', self)
qbtn2.setCheckable(True)
qbtn2.toggled.connect(self.toggleButton)
qbtn2.resize(qbtn2.sizeHint())
hbox.addWidget(qbtn2)
self.setLayout(hbox)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('QProcess controlled by a …Run Code Online (Sandbox Code Playgroud) 在Qt框架中,我们应该能够使用QProcess打开另一个.exe.单击按钮并调用回调时,以下内容无效:
void MainWindow::on_pushButton_clicked()
{
QProcess *process = new QProcess(this);
QString wordPath = "C:/Program Files/Internet Explorer/iexplore.exe";
process->start(wordPath);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将process-> start(wordPath)更改为:
process->start(wordPath, QStringList());
Run Code Online (Sandbox Code Playgroud)
这是一个相同功能的重载,它的工作原理.第二个参数应该是传递给您要启动的新进程的参数.我可以使单参数版本工作的唯一方法是在我的PATH变量中有什么东西,因为"explorer.exe"和"msconfig"都有效.这背后的故事是什么,只能使用第二个QStringList(),它只是一个空列表?
在另一个SO问题中,我看到用户专门添加一个空字符串,如下所示:
QString wordPath = "C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE"
process->start(wordPath, QStringList() << "");
Run Code Online (Sandbox Code Playgroud)
我很想知道这背后的原因是什么.
我编写了一个简单的GUI,引导用户完成签出/签入过程,然后在用户单击GUI按钮时运行bash脚本.
我想在GUI中创建一个字段并显示脚本的输出.现在我正在使用system()(stdio)来运行脚本,但是将脚本的输出传递给我gui中的文本字段似乎很乱.
使用QProcess是一种更好的方法吗?如果是这样,我将如何开始?
另外,你会推荐什么Qt Widget /容器?