Question

I have the following code:

#ifdef Q_OS_WIN32
    Sleep(25);
#endif

Written in for Qt on windows. I know this code compiled before but now for some reason it says Sleep was not declared in this scope. There must be some way around this without having to add

#include "windows.h"

Any experiences/advice with this would be great!!

Was it helpful?

Solution

There must be some way around this without having to add

The official MSDN documentation says you need to include it, so please do not avoid it.

It is a good practice to always be explicit for your dependencies to avoid hidden dependency issues which can blow up anytime when some included header file does not do the favor for you anymore to include your dependency transparently.

This is probably the reason what is happening in here, too, some dependencies of yours cleaned up the library not to include unnecessary things or it was just redesigned internally.

Alternatively, this function was available in WinBase.h earlier as per documentation and now that if you switch to a newer Windows, it could also case issues with hidden dependencies.

The lesson to learn is that you wish to be explicit even for the simplest function that is included for sure in other headers you include.

However, you need to learn the difference between

#ifdef Q_OS_WIN32
#include "windows.h"
#endif

and

#ifdef Q_OS_WIN32
#include <windows.h>
#endif

In this case, you should use the latter rather than the former since the latter would be used for system headers like this, whereas the former is normally used for "local" headers in your project.

That being said, you should avoid using WinAPI in your Qt application. This funtcionality is already provided by QThread or the QtTestLib module.

void QThread::msleep(unsigned long msecs) [static]

Forces the current thread to sleep for msecs milliseconds.

and

void QTest::qSleep(int ms)

Sleeps for ms milliseconds, blocking execution of the test. qSleep() will not do any event processing and leave your test unresponsive. Network communication might time out while sleeping. Use qWait() to do non-blocking sleeping.

ms must be greater than 0.

Note: The qSleep() function calls either nanosleep() on unix or Sleep() on windows, so the accuracy of time spent in qSleep() depends on the operating system.

Example:

QTest::qSleep(250);

With Qt 4, you would need to override the protected sleep method of QThread in a subclass and use that. With Qt 5, you could use it directly since it was made public for this reason to avoid the common subclass demanding API.

OTHER TIPS

You shouldn't be using platform specific calls if one is provided via Qt, so in this case you should probably be using QThread::msleep.

To answer your question about why it worked before and not now, I would guess that some header you included at one point included much more than it should have and was cleaned up in Qt 5. This may be frustrating at first, but most likely beneficial in the long run.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top