Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QProcess write

    General and Desktop
    3
    5
    1526
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      shahriar25 last edited by shahriar25

      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?

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • S
          shahriar25 last edited by

          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.

          kshegunov 1 Reply Last reply Reply Quote 0
          • kshegunov
            kshegunov Moderators @shahriar25 last edited by

            @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.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply Reply Quote 0
            • S
              shahriar25 last edited by

              Thank you kshegunov & SGaist. the problem is solved

              1 Reply Last reply Reply Quote 0
              • First post
                Last post