How to use QTProcess with ping command?



  • Hi,

    I am trying to use QT process as;

    // Create QProcess object
    proc = new QProcess();
    proc->start("ping", QStringList() << "-c 10" << QString(www.google.com));
    

    And I am gettig error as;

    ping: Invalid number '10'
    

    Any help please,

    Kind Regards



  • Ok. I found it.

    I shouldn't use "-c 10"... It was simply as;

    QString m_sHostName = "www.google.com";
    
    proc = new QProcess();
    proc->start("ping", QStringList() << QString(m_sHostName));
    

    Kind Regards,


  • Lifetime Qt Champion

    Hi,

    The problem comes from how you prepare the argument list. You should use QStringList() << "-c" << "10" << "www.google.com"



  • @SGaist Thank you, it is working nicely... :)



  • Here is a complete code. Hope helps someone...

    // WIFI PING TEST BUTTON 
    void DialogWIFI::on_PushButtonPing_clicked()
    {
        QString m_sHostName = "www.google.com";
    
        // Clear text
        ui->TextEditResult->clear();
    
        // Create string command and argument
        m_sHostName = ui->LineEditHostName->text();
    
        ui->TextEditResult->append(m_sHostName);
    
        // Create QProcess object
        proc = new QProcess();
        proc->start("ping", QStringList() << "-c" << "3" << QString(m_sHostName));
        QCoreApplication::processEvents();
    
        // Show output
        connect(proc, SIGNAL(readyReadStandardOutput()), this, SLOT(rightMessage()));
        connect(proc, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage()));
    }
    
    
    // Show right message
    void DialogWIFI::rightMessage()
    {
        QByteArray strdata = proc->readAllStandardOutput();
        strdata = strdata.simplified();
        strdata = strdata.trimmed();
    
        ui->TextEditResult->setTextColor(Qt::black);
        ui->TextEditResult->append(strdata);
        ui->TextEditResult->append("<br>");
    
    }
    
    
    // Show wrong message
    void DialogWIFI::wrongMessage()
    {
        QByteArray strdata = proc->readAllStandardError();
        ui->TextEditResult->setTextColor(Qt::red);
        ui->TextEditResult->append(strdata);
    }
    

Log in to reply
 

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