Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Determine OS

    General and Desktop
    4
    8
    17077
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      szh1 last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • A
        anselmolsm last edited by

        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

        Anselmo L. S. Melo (anselmolsm)

        1 Reply Last reply Reply Quote 0
        • S
          szh1 last edited by

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

          1 Reply Last reply Reply Quote 0
          • T
            tobias.hunger last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • S
              szh1 last edited by

              Thanks for the warning.

              1 Reply Last reply Reply Quote 0
              • I
                iunknwn last edited by

                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)

                Vista x64 with Qt 4.8.2 and VS2010

                1 Reply Last reply Reply Quote 0
                • S
                  szh1 last edited by

                  OK. Thanks for the info.

                  1 Reply Last reply Reply Quote 0
                  • T
                    tobias.hunger last edited by

                    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).

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post