我试图理解为什么不需要在我的 QProcess waitForFinished() 和 waitForStarted() 调用可以工作之前将 a.exec() 调用放入以下 Qt 4.8 代码中。我知道 a.exec() 启动事件循环,在我看来,waitFor* 插槽需要在继续执行之前接收信号(即 'started()' 或 'finished()' )。如果事件循环尚未启动,怎么会发生这种情况?
waitForStarted() 的文档:
Blocks until the process has started and the started() signal has been emitted, or until msecs milliseconds have passed.
代码:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Exec the i2c command to get the power button status
QProcess i2cprocess;
i2cprocess.start("/usr/bin/i2cget -f -y 1 0x4b 0x45");
// Wait for it to start
if(!i2cprocess.waitForStarted())
{
qDebug() << "Could not start QProcess to check power button status.";
exit(-1);
}
// Wait for it to finish
bool returnValue = i2cprocess.waitForFinished();
if ( returnValue )
{
QByteArray status = i2cprocess.readAllStandardOutput().trimmed();
bool ok;
quint16 hexValue = status.toUInt(&ok, 16);
qDebug() << "Power button status: " << status << hexValue << (hexValue & 0x01);
// We want LSB
exit(hexValue & 0x01);
}
else
{
qDebug() << "Error, process never completed to check power button status.";
exit(-1);
}
return a.exec();
}