Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QProcess, how can I read standard out immediately ?



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



  • @Msming

    I solved this issue.

    use "-f" instedof "flush".

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

    @Christian-Ehrlicher

    Thank you!


  • Lifetime Qt Champion

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

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

    Why all the conversions here?



  • @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!



  • @Msming

    I solved this issue.

    use "-f" instedof "flush".

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

    @Christian-Ehrlicher

    Thank you!


Log in to reply