How to determine the platform Qt is running on at runtime?

andreas buykx picture andreas buykx · Jan 24, 2009 · Viewed 25.9k times · Source

Is there a (Qt) way to determine the platform a Qt application is running on at runtime?

Answer

Shinnok picture Shinnok · Feb 8, 2011

Intention: While I hate to bring up a question that is almost 2 years old, I think that a good amended answer is valuable to have on record so that others that end up on this question can do it the right way.

I can't help but notice that most of the answers recommend using the Q_WS set of macros to determine the Operating System, this is not a good solution, since Q_WS_* refers to the Windowing System and not the Operating System platform(for eg. X11 can be run on Windows or Mac OS X then what?), thus one should not follow those macros to determine the platform for which the application has been compiled.

Instead one should use the Q_OS_* set of macros which have the precise purpose of determining the Operating System.

The set currently consists of the following macros:

Q_OS_AIX
Q_OS_BSD4
Q_OS_BSDI
Q_OS_CYGWIN
Q_OS_DARWIN
Q_OS_DGUX
Q_OS_DYNIX
Q_OS_FREEBSD
Q_OS_HPUX
Q_OS_HURD
Q_OS_IRIX
Q_OS_LINUX
Q_OS_LYNX
Q_OS_MAC
Q_OS_MSDOS
Q_OS_NETBSD
Q_OS_OS2
Q_OS_OPENBSD
Q_OS_OS2EMX
Q_OS_OSF
Q_OS_QNX
Q_OS_RELIANT
Q_OS_SCO
Q_OS_SOLARIS
Q_OS_SYMBIAN
Q_OS_ULTRIX
Q_OS_UNIX
Q_OS_UNIXWARE
Q_OS_WIN32
Q_OS_WINCE

Reference: http://doc.qt.io/qt-4.8/qtglobal.html

NB: As mentioned by Wiz in the comments, Qt 5 completely removed the Q_WS_* set of macros, thus now all you can use are Q_OS_* ones.