Is there a (Qt) way to determine the platform a Qt application is running on at runtime?
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With