C++ 在windows上使用QProcess启动进程错误:“0”;计时器只能与以QThread“启动的线程一起使用;
我有一个很小的例子,我正在努力工作。最终目标是能够将一些信息传达给正在等待“cin”呼叫的程序。我想这意味着与标准输入有关 在这个阶段,我尝试使用一些Qt对象来帮助我。虽然我没有使用任何其他Qt的东西 我正在尝试的错误示例是:C++ 在windows上使用QProcess启动进程错误:“0”;计时器只能与以QThread“启动的线程一起使用;,c++,multithreading,qt,C++,Multithreading,Qt,我有一个很小的例子,我正在努力工作。最终目标是能够将一些信息传达给正在等待“cin”呼叫的程序。我想这意味着与标准输入有关 在这个阶段,我尝试使用一些Qt对象来帮助我。虽然我没有使用任何其他Qt的东西 我正在尝试的错误示例是: #include <iostream> #include <QtCore/QString> #include <QtCore/QProcess> #include <QtCore/QStringList> int mai
#include <iostream>
#include <QtCore/QString>
#include <QtCore/QProcess>
#include <QtCore/QStringList>
int main() {
QProcess process;
QString prog = "test.exe";
// Starting "test.exe":
process.start(prog);
bool started = process.waitForStarted();
std::cout << started << std::endl;
// test.exe is waiting for cin, so give "2":
bool response = process.write("2\n");
std::cout << response << std::endl;
}
在极少数情况下,您会有一个没有QApplication或QCoreApplication的Qt应用程序。它们启动计时器、事件、信号/插槽所需的事件循环 控制台XML解析器可以是此类无事件应用程序 看一看,例如,这里有一个最小的应用程序:
在子类QWidget或QObject中启动进程。我打赌
QProcess::waitForStarted()
需要一个事件循环才能运行。您可以在调用QApplication
之前尝试实例化它,或者自己手动创建QEventLoop
。
1
QObject::startTimer: Timers can only be used with threads started with QThread
1
QProcess: Destroyed while process ("test.exe") is still running.