user1484717
user1484717

Reputation: 937

How to call an app that expects stdin input from QtGui?

I'm using Ubuntu and Qt Creator 4

I have a .cpp program in the executable form (say abc.out) that I wish to run when I press a button. It contains a number of cin and cout, so I want it to run on a "terminal" (on Ubuntu) so that I am able to input and output values to it. How can I do that?

I've tried system() and also, QProcess p1; p1.start(./abc.out);

Using QProcess, my executable runs but stops at the first cout. It runs on the application output screen in Qt Creator and not on terminal.

For example: I see on application output:

enter name:

When I type the value and press enter here, it doesn't accept the value, but moves to the next line and allows me to type further. I want to run this abc.out file on the terminal. Any ideas would be really helpful.

Upvotes: 0

Views: 911

Answers (3)

The only sensible solution is to launch the target application in a terminal. Whether your own code provides the terminal window or you reuse an existing terminal application is up to you.

Upvotes: 0

Tapio
Tapio

Reputation: 3456

Do you mean Qt Creator 2.4? In any case, on the Projects tab, you should find the Run settings section and from there you can find a "Run in terminal" checkbox. You could also use a Custom Executable option and type there: gnome-terminal --command ./abc.out The exact details can vary a bit as I'm using Qt Creator 2.5.

This should work when launching from Qt Creator, but when you use your app outside the IDE, you need to launch it from terminal and not by double clicking the executable. To fix this, I can think of two ways:

  1. Launch a terminal window from QtGui (something like QProcess::execute("gnome-terminal --command ./abc.out"); ), though the problem is different systems have different terminal names.
  2. Provide a Qt input/text box yourself as part of your GUI which then forwards the user input to the executable (something like myqprocess.write(input_asked_from_user_by_QtGui); ). Here you probably need to know what information to ask the user beforehand. If you want to display the cout output of the started process, you can use the read method and friends of the QProcess.

Upvotes: 1

pwuertz
pwuertz

Reputation: 1325

From your question I assume that you are writing an application that launches other applications using QProcess. Thats fine, but if your subprocess is waiting for data from the standard input it will wait forever since you did not provide any data. The stdin of your parent application cannot be automatically guided to the subprocess. Imagine you are starting two processes from your main app. To which child process should the input go?

If you want to communicate with child processes, you must use the QIODevice methods of QProcess and send/read data from/to that application.

Upvotes: 0

Related Questions