0

我正在对 IP 地址执行 ping 操作,并且我想在 QMessageBox 中显示正在进行 ping 操作。之后,如果收到响应或发生一秒超时,我想关闭 QMessageBox。

代码:

int status;
QByteArray command;
QMessageBox myBox(QMessageBox::Information, QString("Info"), QString("Checking connection"), QMessageBox::NoButton, this);

command.append("ping -w 1 172.22.1.1");
status=system(command);
myBox.setStandardButtons(0);
myBox.exec();
if (0==status){ // Response received
    // Some stuff here...
    myeBox.setVisible(false);
}
else { // Timeout
    // Some other stuff here...
    myBox.setVisible(false);
}

我的猜测是我可能需要为这项任务使用线程,但由于我是 Qt 新手,所以问题可能出在其他任何地方。

编辑:正如@a​​tamanroman 建议的那样,我尝试使用 QProcess,使用信号 void QProcess::finished (int exitCode, QProcess::ExitStatus exitStatus) [signal],如 Qt 参考中所述:

private:
QProcess *process;
//...

      QMessageBox myBox(QMessageBox::Information, QString("Info"), QString("Checking connection"), QMessageBox::NoButton, this);
    QObject::connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &myBox, SLOT(close()));
    command.append("ping -w 1 172.22.1.1");
    process.start(comdand);
        myBox.setStandardButtons(0);
        myBox.exec();

它不工作。myBox 永远不会关闭。怎么了?

4

2 回答 2

0

您应该使用QProcess(启动 ping.exe 并解析输出)或QTcpSocket(自己执行 ping),而不是system()因为它们是 Qt 的一部分,并且可以在 ping 完成时向您发出信号。连接到该信号以隐藏您的QMessageBox.

于 2012-03-29T07:15:33.787 回答
0

在您的编辑中:首先:

QProcess *process; // This is a pointer, you don't need to add "&" in connect
                   // You should have called "process = new QProcess" before...
QMessageBox myBox; // This is an object, you need to add the "&" to connect;

我们拿出第一个&

QObject::connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), &myBox, SLOT(close()));

第二:使用Linux ping 永远不会停止,那么您将永远不会有finished 信号。您可以提供 ping 一些参数,例如计数或等待时间。或启动计时器以停止该过程。

第三:您需要匹配信号和插槽之间的参数以避免警告等。我建议您创建一个本地SLOT“processfinished(int,QProcess::ExitStatus)”,然后调用myBox.Close(),但是“ myBox" 必须来自类才能在结束调用它的方法后引用它。

于 2013-09-11T18:07:30.273 回答