3

One can compile different code depending on the current Qt version:

#if QT_VERSION < 0x050000
.....
#else
.....
#endif

However, Qt4 and Qt5 have different macros for checking the operating system: Q_WS_WIN -> Q_OS_WIN and Q_WS_X11 -> Q_OS_LINUX, respectively. How to add #ifdef macro for certain operating system?

Jonas
  • 6,915
  • 8
  • 35
  • 53
Michael
  • 5,095
  • 2
  • 13
  • 35
  • 1
    `#if defined Q_WS_X11 || defined Q_OS_LINUX` – Bo Persson Aug 21 '17 at 10:35
  • 1
    That or just define your own variable in your `pro` file where you also branch your configuration based on the operating system ([example](https://stackoverflow.com/a/6371554/1559401)) and can than call the variable inside your own header/source file. – rbaleksandar Aug 21 '17 at 10:36
  • 1
    Note that `#if QT_VERSION < 0x050000` is a problem unless you remember to use hex for the sub-components. For example, to check for a version less that 5.10, you must do `#if QT_VERSION < 0x050A00`, *not* `#if QT_VERSION < 0x051000`. Less error-prone, for those who don't automatically think in hexadecimal, is to do `#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0))`. – bhaller Jan 04 '21 at 22:53

1 Answers1

2

You do not need to use QT_VERSION, you can check both versions like this:

#if defined(Q_WS_WIN) || defined(Q_OS_WIN)
// Windows...
#elif defined(Q_WS_X11) || defined(Q_OS_LINUX)
// Linux...
#endif
Jonas
  • 6,915
  • 8
  • 35
  • 53