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, how can I read standard out immediately ?

QProcess, how can I read standard out immediately ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 396 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.
  • M Offline
    M Offline
    Msming
    wrote on last edited by Msming
    #1

    Code:

    void MainWindow::windowInitialize()
    {
    
        proxmarkProcess = new QProcess(this);
    
        proxmarkProcess->setProgram(PROCESS_PATH);
        connect(proxmarkProcess,SIGNAL(started()),this, SLOT(started()));
        connect(proxmarkProcess,SIGNAL(readyReadStandardError()),this, SLOT(readError()));
        connect(proxmarkProcess,SIGNAL(readyReadStandardOutput()),this, SLOT(readOutput()));
        //connect(proxmarkProcess,SIGNAL(readyRead()),this, SLOT(readyRead()));
    
        ui->cbb_Com->addItem(tr("请选择"));
        foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
    
            qDebug() << "Name : " << info.portName();
            qDebug() << "Description : " << info.description();
            qDebug() << "Manufacturer: " << info.manufacturer();
    
            ui->cbb_Com->addItem(info.portName());
        }
    }
    
    
    void MainWindow::on_cbb_Com_currentTextChanged(const QString &arg1)
    {
        qDebug() << "当前串口:"<<arg1 << arg1.size();
    
        if(arg1.contains("COM"))
        {
    
    
            if(proxmarkProcess->state() != QProcess::ProcessState::Running)
            {
                proxmarkProcess->setArguments(QStringList() << arg1 << "flush");
                proxmarkProcess->start();
                qDebug() << proxmarkProcess->arguments();
                proxmarkProcess->waitForStarted();
            }
            else
            {
                proxmarkProcess->kill();
                proxmarkProcess->waitForFinished();
                proxmarkProcess->setArguments(QStringList() << arg1 << "flush");
                proxmarkProcess->start();
                qDebug() << proxmarkProcess->arguments();
                proxmarkProcess->waitForStarted();
            }
    
        }
    }
    void MainWindow::readOutput(const QPrivateSignal s)
    {
        Q_UNUSED(s)
    
         qDebug()<< "readOutput:";
    //    QStringList strList = QString::fromLocal8Bit(proxmarkProcess->readAllStandardOutput().data()).split("\r\n");
    
    //    foreach(const QString &str, strList)
    //    {
    //        qDebug()<< str;
    //    }
    
    }
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        if(QProcess::Running == proxmarkProcess->state())
        {
            qDebug() << "on_pushButton_clicked()";
    
            proxmarkProcess->write( QString("hw version\r\n").toStdString().c_str());
        }
    }
    

    当前串口: "请选择" 3
    Name : "COM10"
    Description : "USB 串行设备"
    Manufacturer: "Microsoft"
    当前串口: "COM10" 5
    started()
    ("COM10", "flush")
    Running
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    readOutput:
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    readOutput:
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    on_pushButton_clicked()
    readOutput:


    Now, I clicked the button 7 times, the readOutput just one called.

    1 Reply Last reply
    0
    • M Msming

      @Christian-Ehrlicher

      I can't find the flush() in QProcess class.

      I don't know what you mean, I can't convert strings in here? How should I do?

      Best Regard!

      M Offline
      M Offline
      Msming
      wrote on last edited by
      #4

      @Msming

      I solved this issue.

      use "-f" instedof "flush".

      proxmarkProcess->setArguments(QStringList() << arg1 << "-f");

      @Christian-Ehrlicher

      Thank you!

      1 Reply Last reply
      2
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Did you try to call QProcess:flush() after write()?

        proxmarkProcess->write( QString("hw version\r\n").toStdString().c_str());

        Why all the conversions here?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        4
        • M Offline
          M Offline
          Msming
          wrote on last edited by
          #3

          @Christian-Ehrlicher

          I can't find the flush() in QProcess class.

          I don't know what you mean, I can't convert strings in here? How should I do?

          Best Regard!

          M 1 Reply Last reply
          0
          • M Msming

            @Christian-Ehrlicher

            I can't find the flush() in QProcess class.

            I don't know what you mean, I can't convert strings in here? How should I do?

            Best Regard!

            M Offline
            M Offline
            Msming
            wrote on last edited by
            #4

            @Msming

            I solved this issue.

            use "-f" instedof "flush".

            proxmarkProcess->setArguments(QStringList() << arg1 << "-f");

            @Christian-Ehrlicher

            Thank you!

            1 Reply Last reply
            2

            • Login

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