【发布时间】:2012-03-29 07:11:42
【问题描述】:
我正在对一个 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,如 Qt 参考中所述,使用 signal void QProcess::finished (int exitCode, QProcess::ExitStatus exitStatus) [signal]:
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 永远不会关闭。怎么了?
【问题讨论】:
标签: c++ qt ping qmessagebox