QT: make a function to pause at some point for some time

I have a problem with QT. I want my program to stop at the place where I define, say, for 3 seconds. I could not do that. I need this because uranium my program generates a file and it is used by a program that I call a bit later. The problem is that the file does not have enough time to create. My code is as follows:

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

Thanks in advance.

+2
qt
Jun 30 '10 at
source share
3 answers

Perhaps you just need to close the recorded file before you call another program:

 QFile f; ... f.close(); 

(This also flushes the internal buffers so that they are written to disk)

+1
Jun 30 '10 at 12:55
source share

Some features:

1) Use another slot for things to do after sleep:

 QTimer::singleShot(3000, this, SLOT(anotherSlot()); ... void MyClass::anotherSlot() { system(...); } 

2) Without another slot, using a local event loop:

 //write file QEventLoop loop; QTimer::singleShot(3000, &loop, SLOT(quit()) ); loop.exec(); //do more stuff 

I would avoid the local event loop and prefer 1), although local event loops can cause a lot of subtle errors (anything can happen during the .exec () loop).

+3
Jun 30 '10 at 13:45
source share

Try void QTest :: qSleep (int ms) or void QTest :: qWait (int ms)

Getting into the source of these features is also useful if you don't want the overhead of QTest.

Additional information at http://doc.qt.io/qt-5/qtest.html#qSleep

+3
Jun 30 '10 at 15:05
source share



All Articles