3

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.

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

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.
windenergy
  • 361
  • 1
  • 4
  • 13
  • 1
    I bet `QProcess::waitForStarted()` needs an event loop in order to function. You might try instantiating a `QApplication` before calling it, or manually creating a `QEventLoop` yourself. – MrEricSir Sep 28 '15 at 22:18

1 Answers1

0

In rare cases you will have a Qt-app without QApplication or QCoreApplication. They start event loop, required for timers, events, signals/slots.

A console XML-parser could be such kind of event-less application.

Take a look e.g. here for a minimal QtCoreApplication app: How do I create a simple Qt console application in C++?

Start your process within a subclassed QWidget or QObject.

Community
  • 1
  • 1
Valentin H
  • 7,240
  • 12
  • 61
  • 111