September 2, 2010

szh1 szh1
Lab Rat
126 posts

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:

  1. if (/* os is not windows */)
  2. {
  3.  // execute code
  4. }

Thanks in advance.

7 replies

September 2, 2010

anselmolsm anselmolsm
Ant Farmer
417 posts

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

  1.   // OS is not Windows
  2.   #ifndef Q_OS_WIN32
  3.   // execute code

More Qt Macros following this link [doc.qt.nokia.com]

 Signature 

Anselmo L. S. Melo (anselmolsm)
www.anselmolsm.org

September 3, 2010

szh1 szh1
Lab Rat
126 posts

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

September 3, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3224 posts

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.

September 3, 2010

szh1 szh1
Lab Rat
126 posts

Thanks for the warning.

September 4, 2010

iunknwn iunknwn
Lab Rat
34 posts

For run-time, I’d look into

QSysInfo [doc.qt.nokia.com]

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

 Signature 

Vista x64 with Qt 4.8.2 and VS2010

September 5, 2010

szh1 szh1
Lab Rat
126 posts

OK. Thanks for the info.

September 6, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3224 posts

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

 
  ‹‹ QSqlQuery and ORACLE style value binding      CSS for a QComboBox ››

You must log in to post a reply. Not a member yet? Register here!