Congratulations to our 2022 Qt Champions!

QProcess write

  • Hi
    I'm trying to run a external c++ file using QProcess in my app. this is the c++ source:

    int main()
    string s;
    cout<<"enter s:";

    I use a QTextEdit to show and get named Terminal this is what happens when the return key is pressed on my textedit:

    void MainWindow::onTerminalReturnPressed()
    QString text = Terminal->toPlainText();



    and this is what happens when there is something to read from process:

    void MainWindow::onProcessReadyRead()

    when I run the app, "enter s" is shown in the textedit. when I enter anything and press return the text nothing happenes (the string doesn't get shown). what am I doing wrong?

  • Lifetime Qt Champion


    You should rather use readAllStandardOutput.

    On a side note, there's not need for these conversion from and to std::string. You can get a QByteArray from QString directly using either toUtf8 or toLatin1.

  • Hi Thank you @SGaist
    I changed the code to show the standard output and the standard error.
    The main problem was with the end lines. apparently std::cin recognizes \n as the end of input in linux.

  • Moderators

    @shahriar25 said:

    apparently std::cin recognizes \n as the end of input in linux.

    Same as windows, same as OSX. In text mode \n is end of line, that is where cin >> will stop reading.

  • Thank you kshegunov & SGaist. the problem is solved

Log in to reply