44# backend constants
55WX = "wx"
66PYSIDE = "qt-pyside"
7+ PYSIDE2 = "qt-pyside2"
78PYQT4 = "qt-pyqt4"
89PYQT5 = "qt-pyqt5"
910
1011# backend module
11- HAVE_PYQT5 , HAVE_PYQT4 , HAVE_PYSIDE , HAVE_WX = False , False , False , False
12+ HAVE_PYQT5 , HAVE_PYQT4 , HAVE_PYSIDE , HAVE_PYSIDE2 , HAVE_WX = False , False , False , False , False
1213
1314# is any backend imported?
1415HAVE_BACKEND = False
@@ -71,6 +72,23 @@ def load_pyside():
7172 return HAVE_PYSIDE
7273
7374
75+ def load_pyside2 ():
76+ """ returns True is PySide2 found, else False
77+ """
78+ global HAVE_PYSIDE2 , QtCore , QtGui , QtWidgets , QtOpenGL
79+
80+ # backend already loaded, dont load another one
81+ if loaded_backend ():
82+ return False
83+
84+ try :
85+ from PySide2 import QtCore , QtGui , QtOpenGL , QtWidgets
86+ HAVE_PYSIDE2 = True
87+ except ImportError :
88+ HAVE_PYSIDE2 = False
89+ return HAVE_PYSIDE2
90+
91+
7492def load_wx ():
7593 """ returns True is wxPython found, else False
7694 """
@@ -111,9 +129,11 @@ def load_any_qt_backend():
111129 pyqt4_loaded = load_backend (PYQT4 )
112130 # finally try to load pyside
113131 if not pyqt4_loaded :
114- pyside_loaded = load_backend (PYSIDE )
115- if not (pyqt5_loaded or pyqt4_loaded or pyside_loaded ):
116- raise AssertionError ("None of the PyQt5 orPtQt4 or PySide backend can be loaded" )
132+ pyside2_loaded = load_backend (PYSIDE2 )
133+ if not pyside2_loaded :
134+ pyside_loaded = load_backend (PYSIDE )
135+ if not (pyqt5_loaded or pyqt4_loaded or pyside2_loaded or pyside_loaded ):
136+ raise AssertionError ("None of the PyQt5, PtQt4, PySide2 or PySide backend can be loaded" )
117137 else :
118138 return True
119139
@@ -124,7 +144,7 @@ def load_backend(backend_str=None):
124144 If no Qt (such as PyQt5, PyQt4 or PySide) backend is found, wx is loaded
125145
126146 The search order for pythonocc compatible gui modules is:
127- PyQt5, PyQt4, PySide, Wx
147+ PyQt5, PyQt4, PySide2, PySide, Wx
128148
129149 Note
130150 ----
@@ -136,7 +156,7 @@ def load_backend(backend_str=None):
136156
137157 specifies which backend to load
138158
139- backend_str is one of ( "qt-pyqt5", "qt-pyqt4", "qt-pyside", "wx" )
159+ backend_str is one of ( "qt-pyqt5", "qt-pyqt4", ""qt-pyside2", qt-pyside", "wx" )
140160
141161 if no value has been set, load the first module in gui module search
142162 order
@@ -145,7 +165,7 @@ def load_backend(backend_str=None):
145165 -------
146166 str
147167 the name of the loaded backend
148- one of ( "qt-pyqt5", "qt-pyqt4", "qt-pyside", "wx" )
168+ one of ( "qt-pyqt5", "qt-pyqt4", "qt-pyside2", "qt- pyside", "wx" )
149169
150170 Raises
151171 ------
@@ -167,7 +187,7 @@ def load_backend(backend_str=None):
167187 return BACKEND_MODULE
168188
169189 if backend_str is not None :
170- compatible_backends = (PYQT5 , PYQT4 , PYSIDE , WX )
190+ compatible_backends = (PYQT5 , PYQT4 , PYSIDE2 , PYSIDE , WX )
171191 if not backend_str in compatible_backends :
172192 msg = "incompatible backend_str specified: {0}\n " \
173193 "backend is one of : {1}" .format (backend_str ,
@@ -202,6 +222,19 @@ def load_backend(backend_str=None):
202222 else :
203223 pass
204224
225+ if backend_str == PYSIDE2 or (backend_str is None and not HAVE_BACKEND ):
226+ if load_pyside2 ():
227+ HAVE_BACKEND = True
228+ BACKEND_MODULE = 'qt-pyside2'
229+ log .info ("backend loaded: {0}" .format (BACKEND_MODULE ))
230+ return BACKEND_MODULE
231+ elif backend_str == PYSIDE2 and not HAVE_BACKEND :
232+ msg = "{0} could not be loaded" .format (backend_str )
233+ log .exception (msg )
234+ raise ValueError (msg )
235+ else :
236+ pass
237+
205238 if backend_str == PYSIDE or (backend_str is None and not HAVE_BACKEND ):
206239 if load_pyside ():
207240 HAVE_BACKEND = True
@@ -230,7 +263,7 @@ def load_backend(backend_str=None):
230263
231264 if not HAVE_BACKEND :
232265 raise ImportError ("No compliant GUI library could be imported.\n "
233- "Either PyQt5, PyQt4, PySide, or wxPython "
266+ "Either PyQt5, PyQt4, PySide2, PySide, or wxPython "
234267 "is required" )
235268
236269
@@ -256,13 +289,13 @@ def get_qt_modules():
256289 raise ValueError ("no backend has been imported yet with "
257290 "``load_backend``... " )
258291
259- if HAVE_PYQT5 or HAVE_PYQT4 or HAVE_PYSIDE :
292+ if HAVE_PYQT5 or HAVE_PYQT4 or HAVE_PYSIDE2 or HAVE_PYSIDE :
260293 return QtCore , QtGui , QtWidgets , QtOpenGL
261294 elif HAVE_WX :
262295 raise ValueError ("the Wx backend is already loaded" )
263296 else :
264297 msg = ("no Qt backend is loaded, hence cannot return any modules\n "
265- "either you havent got PyQt5, PyQt4 or PySide installed\n "
298+ "either you havent got PyQt5, PyQt4, PySide2 or PySide installed\n "
266299 "or you havent yet loaded a backend with the "
267300 "`OCC.Display.backend.load_backend` function" )
268301 raise ValueError (msg )
0 commit comments