28

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

4 Answers 4

65
+100

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 for Qt 4.8:

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 

References:

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You could also add the fact that Qt5 has completely removed all Q_WS_* macros. So Q_OS_* is now the only way to go.
Don't forget #include <QtGlobal>
So instead of Q_WS_X11 I should probably use Q_OS_LINUX, but X11 might be present on Apple and Windows as well. What about Wayland on Linux? So my question is, how do I replace Q_WS_X11 properly with such new macros only specifying the operating system? (I used the Q_WS_X11 to call XInitThreads, which is now an application attribute, I want to turn on only on X11 systems.)
27

Note that the Q_WS_* macros are defined at compile time, but QSysInfo gives some run time details.

To extend gs's function to get the specific windows version at runtime, you can do

#ifdef Q_WS_WIN switch(QSysInfo::windowsVersion()) { case QSysInfo::WV_2000: return "Windows 2000"; case QSysInfo::WV_XP: return "Windows XP"; case QSysInfo::WV_VISTA: return "Windows Vista"; default: return "Windows"; } #endif 

and similar for Mac.

If you are using a Qt version 5.9 or above, kindly use the below mentioned library function to retrieve correct OS details, more on this can be found here. There is also a QSysInfo class which can do some additional functionalities.

#ifdef Q_WS_WIN #include <QOperatingSystemVersion> switch(QOperatingSystemVersion::current()) { case QOperatingSystemVersion::Windows7: return "Windows 7"; case QOperatingSystemVersion::Windows8: return "Windows 8"; case QOperatingSystemVersion::Windows10: return "Windows 10"; default: return "Windows"; } #endif 

1 Comment

Deprecated since Qt 5. Shinnok's answer is the right one.
2

For Qt5 I use the following:

logging.info("##### System Information #####") sysinfo = QtCore.QSysInfo() logging.info("buildCpuArchitecture: " + sysinfo.buildCpuArchitecture()) logging.info("currentCpuArchitecture: " + sysinfo.currentCpuArchitecture()) logging.info("kernel type and version: " + sysinfo.kernelType() + " " + sysinfo.kernelVersion()) logging.info("product name and version: " + sysinfo.prettyProductName()) logging.info("#####") 

Documentation: http://doc.qt.io/qt-5/qsysinfo.html

Comments

0

Here is part of my code to detect windows or mac at run time and the version

 #include <QSysInfo> #include <QOperatingSystemVersion> auto OSType= OSInfo.type(); auto OSInfo = QOperatingSystemVersion::current(); if (OSType !=1) //not windows os { return 0; } if (OSInfo < QOperatingSystemVersion::Windows7) // less than win7 { return 0; } 

1 Comment

The declarations should be in reverse order.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.