Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess write

QProcess write

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.1k Views
  • 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 Offline
    S Offline
    shahriar25
    wrote on last edited by shahriar25
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • S Offline
        S Offline
        shahriar25
        wrote on last edited by
        #3

        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.

        kshegunovK 1 Reply Last reply
        0
        • S shahriar25

          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.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @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
          0
          • S Offline
            S Offline
            shahriar25
            wrote on last edited by
            #5

            Thank you kshegunov & SGaist. the problem is solved

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved