28

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

Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
andreas buykx
  • 12,608
  • 10
  • 62
  • 76

4 Answers4

65

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.

david
  • 1,311
  • 12
  • 32
Shinnok
  • 6,279
  • 6
  • 31
  • 44
  • 8
    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. – Wiz Mar 21 '13 at 18:02
  • 1
    Don't forget #include – paulm Sep 22 '14 at 11:21
  • 1
    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.) – math Aug 11 '15 at 15:03
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
Srijan Chaudhary
  • 637
  • 1
  • 8
  • 18
Reed Hedges
  • 1,590
  • 2
  • 15
  • 17
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

sunyata
  • 1,843
  • 5
  • 27
  • 41
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;
        }
Sherif O.
  • 506
  • 4
  • 15