2

I'm porting some code from C++/Qt to Python/PyQt.

What do I do with

#ifdef Q_OS_WIN
    ...
#else

Is there a PyQt equivalent of the Qt macro Q_OS_WIN?

Johan Råde
  • 20,480
  • 21
  • 73
  • 110

3 Answers3

5

Why you ever want to do this instead of using Python sys.platform from sys module?

import sys
if sys.platform == 'win32':
    print("win")
else:
    print("winner!")

I have to mention that win32 is the same even if you run on x64 python.

sorin
  • 161,544
  • 178
  • 535
  • 806
  • I just wanted to know if there is a PyQt API for detecting the OS. Apparently there is not. – Johan Råde May 29 '11 at 08:08
  • And if you think about this, it makes sense to not have one, when the language itself gives you the same functionality. – sorin May 29 '11 at 08:17
0

To test if the underlying operating system is a macintosh

import PyQt4.QtGui
MAC = hasattr(PyQt4.QtGui, "qt_mac_set_native_menubar")

To test if the underlying operating system is Linux/X11, BSD, Solaris

import PyQt4.QtGui
X11 = hasattr(PyQt4.QtGui, "qt_x11_wait_for_window_manager")

From Mark Summerfield's book

I don't know of a specific Windows test but if the two above returned None then it's likely the system is windows.

cmoman
  • 341
  • 3
  • 5
0

Unlike C++, there is no preprocessor in Python. This means that you can't completely exclude blocks of code from being seen by the compiler/interpreter.

For most practical purposes, however, it is sufficient to place the relevant block of code inside a Python if statement.

Take a look at Python: What OS am I running on?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012