简体   繁体   English

使用Windows上的QProcess启动进程错误:“计时器只能用于以QThread启动的线程”

[英]Start process with QProcess on windows error: “Timers can only be used with threads started with QThread”

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. 最终目标是能够将某些信息传递给正在等待“cin”呼叫的程序。 I guess that means something to do with Standard Input. 我想这意味着与标准输入有关。

I am trying to use some Qt objects to help me at this stage. 我想在这个阶段使用一些Qt对象来帮助我。 Although I am not using any other Qt stuff. 虽然我没有使用任何其他Qt的东西。

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;
}

Here is the error messages: 这是错误消息:

1
QObject::startTimer: Timers can only be used with threads started with QThread
1
QProcess: Destroyed while process ("test.exe") is still running.

In rare cases you will have a Qt-app without QApplication or QCoreApplication. 在极少数情况下,您将拥有没有QApplication或QCoreApplication的Qt-app。 They start event loop, required for timers, events, signals/slots. 它们启动事件循环,定时器,事件,信号/插槽所需。

A console XML-parser could be such kind of event-less application. 控制台XML解析器可以是这种无事件应用程序。

Take a look eg here for a minimal QtCoreApplication app: How do I create a simple Qt console application in C++? 看看这里的最小QtCoreApplication应用程序: 如何在C ++中创建一个简单的Qt控制台应用程序?

Start your process within a subclassed QWidget or QObject. 在子类QWidget或QObject中启动您的进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM