Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Determine OS
QtWS25 Last Chance

Determine OS

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 17.5k Views
  • 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 Offline
    S Offline
    szh1
    wrote on last edited by
    #1

    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
    0
    • A Offline
      A Offline
      anselmolsm
      wrote on last edited by
      #2

      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
      0
      • S Offline
        S Offline
        szh1
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tobias.hunger
          wrote on last edited by
          #4

          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
          0
          • S Offline
            S Offline
            szh1
            wrote on last edited by
            #5

            Thanks for the warning.

            1 Reply Last reply
            0
            • I Offline
              I Offline
              iunknwn
              wrote on last edited by
              #6

              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
              0
              • S Offline
                S Offline
                szh1
                wrote on last edited by
                #7

                OK. Thanks for the info.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tobias.hunger
                  wrote on last edited by
                  #8

                  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
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved