我正在对 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 新手,所以问题可能出在其他任何地方。
编辑:正如@atamanroman 建议的那样,我尝试使用 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 永远不会关闭。怎么了?