QProcess waitForFinished() および waitForStarted() 呼び出しが機能する前に、次の Qt 4.8 コードに a.exec() 呼び出しを配置する必要がない理由を理解しようとしています。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();
}