[ACCEPTED]-How to determine the platform Qt is running on at runtime?-cross-platform

Accepted answer
Score: 65

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

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

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

The set currently consists 4 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 3 mentioned by Wiz in the comments, Qt 5 completely 2 removed the Q_WS_* set of macros, thus now 1 all you can use are Q_OS_* ones.

Score: 27

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

To extend gs's function 7 to get the specific windows version at runtime, you 6 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 5 a Qt version 5.9 or above, kindly use the 4 below mentioned library function to retrieve 3 correct OS details, more on this can be 2 found here. There is also a QSysInfo class which can 1 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
Score: 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

0

Score: 0

Here is part of my code to detect windows 1 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;
        }

More Related questions