How to Create a Pause/Wait Function Using Qt

How do I create a pause/wait function using Qt?

This previous question mentions using qSleep() which is in the QtTest module. To avoid the overhead linking in the QtTest module, looking at the source for that function you could just make your own copy and call it. It uses defines to call either Windows Sleep() or Linux nanosleep().

#ifdef Q_OS_WIN
#include <windows.h> // for Sleep
#endif
void QTest::qSleep(int ms)
{
QTEST_ASSERT(ms > 0);

#ifdef Q_OS_WIN
Sleep(uint(ms));
#else
struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
nanosleep(&ts, NULL);
#endif
}

Run a function with a delay in QT

my suggestion is to read first why sleep are a bad idea, specially when GUIS are involved..
anyways, you can since qt5

Functions of QThread

void msleep(unsigned long msecs);
void sleep(unsigned long secs);
void usleep(unsigned long usecs);

using this in your code, you can do

void MainWindow::my_function(){
// Here I need something to make a delay of 2 secs
sleep(2);
qDebug() << "result";
}

but as I said before, read 1st, because this will work, but will freeze the main window too, which is a not so nice idea when considering User Experience etc

Sleep in Qt 5.6

Because it makes no sense, if one thread could command another thread to sleep,
there is only one single possibility to sleep:
you can only force the current thread to sleep with the static QThread::sleep() method.

How to give a delay in loop execution using Qt

EDIT (removed wrong solution).
EDIT (to add this other option):

Another way to use it would be subclass QThread since it has protected *sleep methods.

QThread::usleep(unsigned long microseconds);
QThread::msleep(unsigned long milliseconds);
QThread::sleep(unsigned long second);

Here's the code to create your own *sleep method.

#include <QThread>    

class Sleeper : public QThread
{
public:
static void usleep(unsigned long usecs){QThread::usleep(usecs);}
static void msleep(unsigned long msecs){QThread::msleep(msecs);}
static void sleep(unsigned long secs){QThread::sleep(secs);}
};

and you call it by doing this:

Sleeper::usleep(10);
Sleeper::msleep(10);
Sleeper::sleep(10);

This would give you a delay of 10 microseconds, 10 milliseconds or 10 seconds, accordingly. If the underlying operating system timers support the resolution.

how to pause the program for some seconds?

If you are using Qt5 you can use the following trick:

QMutex mut;
mut.lock();
mut.tryLock(milliseconds);
mut.unlock(); // I am not sure if this is a necessity

With Qt4 you can use QWaitCondition::wait() on the mutex;

Keep in mind that if this is in your interface thread your gui will freeze until the interval ellapses.

Qt - What is the correct way to pause execution of a program to wait for user input?

I am not entirely sure on what your end result is meant to be, but when working with UI's, you would normally never try to wait for anything in your own code block / loop. Let QT / the other framework handle how these loops need to work, which may or may not include blocking "modal" dialogues.

Instead you just wait for things (events) to happen. In this case you likely want to wait for QT to tell you that the user clicked on some coordinate in the image, then do whatever you want to do in response.

If you care about the user trying to close the window before completing the task, there will be an event for that as well.

Not real code.

//measurements may want to be a UI thing, such as a table or whatever
//is suitable for your use . You would then also have the ability to go
//back and make corrections, etc.
measurements.push_back(Measurement("Measurement Foo"));
measurements.push_back(Measurement("Measurement Bar"));
currentMeasurement = 0;
myImage->addMouseDownHandler(std::bind(&MyApp::onImageClickedOn, this));
...

void MyApp::startMeasurement()
{
auto &measurement = measurements[currentMeasurement];
delete fromMarker; fromMarker = nullptr;
instructionCtrl->setText(
"Please select two points to define " +
measurement.getName());
}
void MyApp::onImageClickedOn(MouseEvent event)
{
if (!fromMarker)
{ //Create a marker where the from/start point is
//Likely wants to be visual/ui object to aid user
fromMarker = new FromSelectMarker(myImage, event.mousePos);
}
else
{
//Second point
auto from = fromMarker.getPos();
auto to = event.mousePos;
auto &measurement = measurements[currentMeasurement];
measurement.setPositions(from, to);

++currentMeasurement;
if (currentMeasurement < measurements.size()
{
startMeasurement();
}
else
{
//finished, do next thing
}
}

}

If you really have a complex sequence of users going through a number of different finite "states" with some form of navigation, then maybe a FSM framework is what you want. Or if you don't have a simple one available, you can just tidy up the code to get something similar.

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

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)

QT C++ wait till specific time to execute function

Short answer:

Change:

now.currentDateTime();

to

now = QDateTime::currentDateTime();

Longish answer:

currentDateTime() is a static function which instead of changing your existing object, actually returns a new QDataTime object. Although you are calling it as a member function, it's still called as a static one and leaves your object intact, which is still invalid.

Your later call to secsTo() on an invalid data time probably gets you an negative or really large number that either has passed (never going to trigger) or really late in the future.

In Qt how can I delay a member function's return until a signal is received?

For what it's worth, in the end I decided that the best route was to implement more message passing code - wait for the user input to dispatch a message. It was longer code than if I had used/had available the 'traditional' call back type paradigm, but it worked cleanly in the end.



Related Topics



Leave a reply



Submit