在Windows错误上使用QProcess启动进程:“计时器只能与以QThread开头的线程一起使用" [英] Start process with QProcess on windows error: "Timers can only be used with threads started with QThread"
问题描述
我有一个最小的例子,我正在努力工作.最终目标是能够将一些信息传达给正在等待"cin"调用的程序.我想这与标准输入有关.
I have a minimal example i am trying to get working. The end goal is to be able to communicate some information to a program that is waiting on a "cin" call. I guess that means something to do with Standard Input.
在此阶段,我正在尝试使用一些Qt对象来帮助我.尽管我没有使用任何其他Qt东西.
I am trying to use some Qt objects to help me at this stage. Although I am not using any other Qt stuff.
我正在尝试给我带来错误的示例是:
The example I am trying that gives me errors is:
#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;
}
以下是错误消息:
1
QObject::startTimer: Timers can only be used with threads started with QThread
1
QProcess: Destroyed while process ("test.exe") is still running.
推荐答案
在极少数情况下,您将拥有一个没有QApplication或QCoreApplication的Qt应用程序.它们启动事件循环,这是计时器,事件,信号/插槽所必需的.
In rare cases you will have a Qt-app without QApplication or QCoreApplication. They start event loop, required for timers, events, signals/slots.
控制台XML解析器可能是这种无事件的应用程序.
A console XML-parser could be such kind of event-less application.
例如这里是最小的QtCoreApplication应用程序:如何进行在C ++中创建一个简单的Qt控制台应用程序?
Take a look e.g. here for a minimal QtCoreApplication app: How do I create a simple Qt console application in C++?
在子类化的QWidget或QObject中启动您的过程.
Start your process within a subclassed QWidget or QObject.
这篇关于在Windows错误上使用QProcess启动进程:“计时器只能与以QThread开头的线程一起使用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!