QT实现Ping主机IP地址

时间:2025-05-09 07:37:50

void Ping(QString strPingIP)//strPingIP 对方IP地址

{  

    //QString strPingIP = "192.168.1.88";
    QProcess pingProcess;
    QString strArg = "ping " + strPingIP + " -n 1 -i 2";  //strPingIP 为设备IP地址
    (strArg, QIODevice::ReadOnly);
    (-1);

    QString p_stdout = QString::fromLocal8Bit(());

    qDebug() << p_stdout;
    bool bPingSuccess = false;

    if (p_stdout.contains("TTL=")) //通过特殊字符串进行判断ping是否成功
    {
        printf("Ping true\n");
        bPingSuccess = true;
    }
    else
    {
        printf("Ping false\n");
        bPingSuccess = false;
    }
    return;

}