Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Determine OS



  • I am not sure if I am posting in the right section.

    Is there a way to find out what operating system my program is running on? I have some code that doesn't work on windows, and I want to do something like:

    @
    if (/* os is not windows */)
    {
    // execute code
    }
    @

    Thanks in advance.



  • You can disable this code in compile time. For example:

    @
    // OS is not Windows
    #ifndef Q_OS_WIN32
    // execute code
    @

    More Qt Macros following "this link":http://doc.qt.nokia.com/4.6/qtglobal.html



  • Thanks a lot! I have been trying to work out this problem for a while. Much appreciated.


  • Moderators

    Please note that the suggestion of anselmolsm is applied at compile time (so the compiler on windows will not even see the code after the #ifdef at all) while your "if-approach" is evaluated at run time (The compiler sees the code and the application will decide whether it is running on windows or not when run).

    In practice that does not make much of a difference, but it still something to keep in the back of your head.



  • Thanks for the warning.



  • For run-time, I'd look into

    "QSysInfo":http://doc.qt.nokia.com/4.6/qsysinfo.html

    Again, under the hood they are relaying on macros anyways, (i think)



  • OK. Thanks for the info.


  • Moderators

    iunknwn: Of course this relies on macros under the hood:-)

    Runtime detection is usually not what you want in the first place: Usually you need some code to do something platform specific and that code will not compile anywhere but on that one platform. You need macros for that or you will get into trouble everywhere that platform specific code does not work.

    Runtime detection is basically only useful if you need to know the exact version of the OS (e.g. Windows 7) you are running on and not just the generic type of OS (e.g Windows).


Log in to reply