QT:创建一个函数,在某个时刻暂停一段时间

QT:创建一个函数,在某个时刻暂停一段时间,qt,Qt,我的QT有问题。 我想让我的程序停在我定义的地方,比如说3秒钟。我没办法做到。我需要它,因为我的程序最早生成一个文件,它被我稍后调用的程序使用。问题是,该文件似乎没有足够的时间创建。我的代码如下所示: void MainWindow::buttonHandler() { QFile ..... (creating a text file); //Making a stream and writing something to a file //A place where

我的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;
}

提前感谢。

在调用其他程序之前,您可能只需要关闭写入的文件:

QFile f;
...
f.close();
(这也会刷新内部缓冲区,以便将其写入磁盘)

一些可能性:

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()期间,任何事情都可能发生)。

尝试void QTest::qSleep(int-ms)或void QTest::qWait(int-ms)

如果您不想增加QTest的开销,那么查看这些函数的源代码也很有用


更多信息请访问

谢谢,这很有帮助。愚蠢的我。但事先,我如何真正暂停程序(功能)?