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:";
    cin>>s;
    cout<<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();

    process->write(QByteArray::fromStdString(text.toStdString()));
    process->waitForBytesWritten();
    

    }

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

    void MainWindow::onProcessReadyRead()
    {
    Terminal->append(process->readAll());
    }

    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

    Hi,

    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.


  • Qt Champions 2016

    @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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.