我有 QT 问题。
我想让我的程序在我定义的地方停止,比如说 3 秒钟。我没能做到这一点。我需要它,因为早些时候我的程序生成文件,并且它被我稍后调用的程序使用。问题是,该文件似乎没有足够的时间来创建。我的代码如下所示:
void MainWindow::buttonHandler()
{
QFile ..... (creating a text file);
//Making a stream and writing something to a file
//A place where program should pause for 3 seconds
system("call another.exe"); //Calling another executable, which needs the created text file, but the file doesn`t seem to be created and fully written yet;
}
提前致谢。
一些可能性:
1)使用另一个槽来处理睡眠后要做的事情:
QTimer::singleShot(3000, this, SLOT(anotherSlot());
...
void MyClass::anotherSlot() {
system(...);
}
2) 没有另一个槽,使用本地事件循环:
//write file
QEventLoop loop;
QTimer::singleShot(3000, &loop, SLOT(quit()) );
loop.exec();
//do more stuff
我会避免本地事件循环并更喜欢 1),但是本地事件循环可能会导致大量微妙的错误(在loop.exec()期间,任何事情都可能发生)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)