tags:

views:

58

answers:

3

Hello, I've got a QT problem. I want to make my program stop at place where I define, let`s say for 3 seconds. I couldn't manage to do that. I need that because earler my program generates file and it is used by a program which I call a bit later. Problem is, that file doesn't seem have enough time to create. My code looks like this:

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.

+1  A: 

Maybe you just need to close the written file before you call the other program:

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

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

sth
Thanks, that helped. Stupid me.But in advance, how do I actually pause the program (function)?
EdgeLuxe
+1  A: 

Some possibilities:

1) Use another slot for the things to do after the 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 local event loop and prefer 1) though, local event loops can cause a plethora of subtle bugs (During the loop.exec(), anything can happen).

Frank
Thank you very much!
EdgeLuxe
+1  A: 

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

Looking into the source of these functions is also useful if you do not want the overhead of QTest.

More info at http://doc.qt.nokia.com/4.2/qtest.html#qSleep

Chris
Thank you very much!
EdgeLuxe