Nominate our 2022 Qt Champions!

Interaction between a terminal and a window

  • Hi,

    I have an application composed by two different parts: a window and a terminal.
    The terminal interacts with my window : for instance the terminal asks to a user the number of the shape he wants to display on the window, the user enters the number and the terminal asks the window to display the shape which has the number submitted by the user.
    My question is: how can my terminal interact with my window? I am on Ubuntu with Qt Creator.
    I'd like my application launches a terminal and a window when it is launched.
    I don't know how to run a part of my program in a terminal and the other part in a window. For exemple I'd like to do something like this :

    @#include <iostream>
    #include <QApplication>

    using namespace std;

    int main(int argc, char* argv[])
    int number;

    //Create a window
    QApplication app(argc, argv);
    MainWindow window(900, 700, QString("Window"));;

    //On the terminal
    cin >> number;

    //Display shape on the window

    return app.exec();

    But I have an error due to std::cout. I guess it's because I don't have a standard out in a terminal...

    Thanks for your help!
    Have a nice fay.

  • Just to clarify, but why do you need the terminal?? Isn't a combobox (shape) much easier. Using the signal/slot mechanism to pass it along to other system parts is basic stuff.

  • I can't use a graphical interface for this part. It's mentionned in the specifications of my project. :/

  • Lifetime Qt Champion


    Then make two applications (one CLI, one GUI) and use IPC to make one interact with the other.

  • I can't do this in an other way?
    I saw we can specify a terminal for in/out in Environment->Terminal. If I specify one terminal in this field can I do what I want?
    I tried to specify a terminal but when I launch my app no terminal is launched...


  • You can tick the "run in terminal" checkbox in Projects->Run Settings. Is that what you want?

    Note that your code will not work as you expect (I guess): It will wait for a number before it even draws your window.

  • Yes, thanks Toblas. ;)

    You are right, because of the QApplication::exec() my app doesn't work.
    My terminal waits for my window is closed or my loop blocks the exec() of my window. But I want the two windows (terminal and displaying zone of my shape) in parallel.

    I think I have to use QThread to do what I want. I guess someting like that :

    • I have a general class which contains an instance of my terminal and an instance of my window which displays shapes
    • the loop of my terminal is performed by a QThread to allow my window displays shapes during the loop

    Am I right?

    I have an other question about the terminal. How can I display a terminal in a new Window with its content produced by a QThread (I/O) ?

    Thanks for your help!

  • maybe create two different applications (one with gui the other as terminal) then use QProcess in gui to call terminal and the read/write terminal ? QProcess acts as standard QIODevice.

  • Yup, if you create your console process via QProcess, you can write to its STDIN via the QProcess object and also read its STDOUT and STDERR that way. That should allow for basic interaction.

    A more sophisticated method, which also works if the console application is not created via QProcess (it may already be running), would be something based on QSharedMemory and QSystemSemaphore. That's what I usually do to implement communication between several instances of my application. Works fine for me.

  • OK. So if I create a QProcess that launches a new terminal and executes a program (my loop which asks the number to the user), can my executed program in the QProcess can interact with the QProcess and the QProcess interact with my "general app"? For instance if I want my executed program in the QProcess returns a list of numbers to my general app. Can I use signals/slots or something else to do the connection between the different components?


  • Your GUI application ("general app") would launch the Console application as a new process via QProcess. Then the GUI app controls the Console application/process via the QProcess object. More specifically, everything the Console app writes to its STDOUT or STDERR, i.e. the output that normally would appear as Text on the Console window, will go straight to the QProcess object and can be read and processed from there, by the GUI application. It's also possible to write to the Console process' STDIN via the QProcess object, to send some data "back", which is like a user would type into the Console window...

    QProcess has useful readyReadStandardOutput() and readyReadStandardError() signals, which will tell you when new output from the Console app has become available...

  • Lifetime Qt Champion

    If you want to use QProcess to pilot your second program, the only option is to parse the output from QProcess (using readyRead for example to know when new data has come out of your CLI) and act accordingly.

    If you wan't something more sophisticated, you'll need some form of IPC.

  • I think I'm going to use IPC system.
    So I can create shared memory with QSharedMemory class.
    But can I share a memory segment created with QSharedMemory object with a process that doesn't use Qt classes? (so it uses shm* functions). Because when I set a key to my QSharedMemory, the type of the key is QString.

  • See here:

    (It's about a QSystemSemaphore, but the point is exactly the same)

    EDIT: Link fixed.

  • Do you want a terminal or just an area where the user can enter text?

  • Thanks, MuldeR.
    Tobias Hunger, I need a terminal. ;)

    I have another question : if I share memory between the two components, how can the GUI be notified that the value of the shared memory has changed? For exemple the terminal writes a list of numbers in this shared memory and changes it : my GUI needs to load the new list of numbers.

Log in to reply