在窗口错误中使用QProcess启动进程:"Timers can only be used with threads started with QThread"

Start process with QProcess on windows error: "Timers can only be used with threads started with QThread"

本文关键字:with only can Timers be used threads QThread started 错误 窗口      更新时间:2023-10-16

我有一个最小的例子,我试图得到工作。最终目标是能够将一些信息传递给正在等待"cin"调用的程序。我猜这意味着与标准输入有关。

我正试图使用一些Qt对象来帮助我在这个阶段。虽然我没有使用任何其他Qt的东西。

我正在尝试给我错误的例子是:

#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("2n");
    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应用程序。它们启动事件循环,需要计时器,事件,信号/槽。

控制台xml解析器可以是这种无事件的应用程序。

看看这里的一个最小的QtCoreApplication应用程序:我如何在c++中创建一个简单的Qt控制台应用程序?

在子类QWidget或QObject中启动进程。