0

I have a simple QMainWindow in which user can set some parameters. I have three buttons which, when clicked, create a new QProcess displaying a QMainWindow. For example, main window's button A starts QProcess A, main window's button B starts process B, etc. I'd like to be able to interact with the main window, because now when I click the button the QProcess starts correctly, displaying another window etc, but the original main window stays frozen until the QProcess ends.

Is there a way to maintain the main window responsive, in order to be possible to interact with it WHILE the QProcess/QProcesses runs/run?

EDIT: here's how I start processes:

QProcess process;
process.execute("../../RRTStar/RRTStar", QStringList() << "--file" << "../../settings.conf");

where RRTStar is the name of the executable and --file ../../settings.conf are the command line parameters. Note that RRTStar is composed of a MainWindow and runs heavy computation using threads.

Michael
  • 876
  • 9
  • 29
  • Show us how do you start processes? – Evgeny Jan 22 '16 at 17:25
  • Qt has very good docs. If your favorite editor/IDE doesn't support context sensitive help with Qt code, you should run Qt Creator on the side, so you can, for example, in this case, put cursor to method name `execute()`, hit F1, and then read the documentation, quoted in that answer below. – hyde Jan 22 '16 at 18:27
  • You didn't understand my question, it seems. I don't need a way to interact with the `QProcess`, that already works, since the new process window is responsive. The problem is that the main window stays frozen until the window process has finished and is up. There doesn't seem to be a way to solve this, all docs and questions are related to QDialog/QMessageBox modality. – Michael Jan 23 '16 at 14:49

1 Answers1

2

Information from the official documentation:

int QProcess::execute(const QString & program, const QStringList & arguments)

Starts the program program with the arguments arguments in a new process, waits for it to finish

If you want to make asynchronous non-blocking call, you need to use QProcess::start():

void QProcess::start(const QString & program, const QStringList & arguments, OpenMode mode = ReadWrite)

About an interaction with the process: it inherites QIODevice, so

QProcess allows you to treat a process as a sequential I/O device. You can write to and read from the process just as you would access a network connection using QTcpSocket. You can then write to the process's standard input by calling write(), and read the standard output by calling read(), readLine(), and getChar(). Because it inherits QIODevice...

process.write("Qt rocks!");
//...
QByteArray result = process.readAll();
Vladimir Bershov
  • 2,701
  • 2
  • 21
  • 51
  • I already tried this before asking the question. `QProcess::start()` correctly start the process and, since it takes a lot of time to finish, it immediately terminates it without waiting for it to be finished. I added a `waitForFinished()`, but of course that doesn't work, since 1) I don't know how much it will take to finish and 2) it again freezes the main window. So, any suggestions? – Michael Jan 23 '16 at 14:44
  • I do not want to communicate with the process. I want it to freely run as long as it takes, and I also want the main window to be responsive. – Michael Jan 23 '16 at 14:55
  • 1
    Your process may terminate because of you create it (the process obj) in a local scope. Try to make the process object a class member or to use process = new QProcess();. You must be shure the the process's object has long life – Vladimir Bershov Jan 23 '16 at 15:03