Results 1 to 7 of 7

Thread: Detect which OS?

  1. #1
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Detect which OS?

    How can my program find out which Operating System it is running on?

    I have an app that compiles on Linux, Windows and OSX, but obviously it needs to some things differently on each (like the location is saves its settings for example).

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Detect which OS?

    Take a look at Q_OS_* macros.

  3. #3
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Detect which OS?

    That looks hopeful, but how do I use a pre-processor directive in normal C++ code (and not in the header).. sorry if that's a bit of a noob question, never needed to do that before.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Detect which OS?

    Qt Code:
    1. void myCustomFunc(){
    2. #ifdef Q_OS_LINUX
    3. linuxNativeCode()
    4. #endif
    5. //...
    6. #ifdef Q_OS_WIN32
    7. winApiCalls()
    8. #endif
    9. }
    To copy to clipboard, switch view to plain text mode 

  5. The following 2 users say thank you to wysota for this useful post:

    December (20th May 2007), WinchellChung (19th September 2007)

  6. #5
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Detect which OS?

    Wow, that was easy, exactly what I needed.

    Thanks loads for the help.

  7. #6
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Detect which OS?

    Qt has a lot of this already done. For application settings, for example, you can use the QSettings class.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  8. The following user says thank you to Michiel for this useful post:

    December (20th May 2007)

  9. #7
    Join Date
    Mar 2006
    Posts
    74
    Thanks
    1
    Qt products
    Qt3
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Detect which OS?

    To add to that there are many that consider using conditional compilation to be "evil". So you should avoid this if at all possible. In particular Qt has a number of things that hide differences like this in high level libraries. QSettings is an example. So using QSettings Qt will take care of using the registry on Windows, Carbon on Mac and will use a text file on Linux/Unix... for user setting and your code for all of these platforms will be the same or very nearly the same.

    So if you find yourself thinking that you will need to use conditional compilation to handle something ask here first and in most cases someone will be able to tell you which Qt objects to use so that you can avoid this. In the end your code will be simpler and easier to understand and maintain. This is not always possible but you should only have to use conditionals under a very limited number of cases and most of those will involve things that are at a very low level (typically hardware level stuff).

Similar Threads

  1. How to detect a process ?
    By Nyphel in forum Newbie
    Replies: 2
    Last Post: 19th April 2007, 13:28
  2. Replies: 1
    Last Post: 9th March 2007, 21:07
  3. how to detect a keystroke
    By irfanhab in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2006, 08:05
  4. Detect utf8 text/html bool....
    By patrik08 in forum Newbie
    Replies: 2
    Last Post: 2nd June 2006, 11:39
  5. how to detect key events on a list box?
    By mahe2310 in forum Qt Programming
    Replies: 19
    Last Post: 9th March 2006, 05:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.