11
11
on_rtd = os .environ .get ('READTHEDOCS' , None ) == 'True'
12
12
13
13
if not on_rtd : # pragma: no cover
14
- try :
15
- import PySide .QtCore as _QtCore
16
- QtCore = _QtCore
17
- USING_PYSIDE = True
18
- except ImportError :
19
- USING_PYSIDE = False
20
-
21
- FORCE_PYQT = os .environ .get ('PYTEST_QT_FORCE_PYQT' , 'false' ) == 'true'
22
- if not USING_PYSIDE or FORCE_PYQT :
14
+
15
+ def _try_import (name ):
23
16
try :
24
- import sip
17
+ __import__ (name )
18
+ return True
25
19
except ImportError :
26
- msg = 'pytest-qt requires either PyQt4 or PySide to be installed'
20
+ return False
21
+
22
+ def _guess_qt_api ():
23
+ if _try_import ('PySide' ):
24
+ return 'pyside'
25
+ elif _try_import ('PyQt4' ):
26
+ return 'pyqt4'
27
+ elif _try_import ('PyQt5' ):
28
+ return 'pyqt5'
29
+ else :
30
+ msg = 'pytest-qt requires either PySide, PyQt4 or PyQt5 to be installed'
27
31
raise ImportError (msg )
28
- USING_PYSIDE = False
29
- import PyQt4 .QtCore as _QtCore
30
- QtCore = _QtCore
31
-
32
- if USING_PYSIDE :
33
- def _import_module (moduleName ):
34
- pyside = __import__ ('PySide' , globals (), locals (), [moduleName ], 0 )
35
- return getattr (pyside , moduleName )
36
-
32
+
33
+ # backward compatibility support: PYTEST_QT_FORCE_PYQT
34
+ if os .environ .get ('PYTEST_QT_FORCE_PYQT' , 'false' ) == 'true' :
35
+ QT_API = 'pyqt4'
36
+ else :
37
+ QT_API = os .environ .get ('PYTEST_QT_API' )
38
+ if QT_API is not None :
39
+ QT_API = QT_API .lower ()
40
+ if QT_API not in ('pyside' , 'pyqt4' , 'pyqt5' ):
41
+ msg = 'Invalid value for $PYTEST_QT_API: %s'
42
+ raise RuntimeError (msg % QT_API )
43
+ else :
44
+ QT_API = _guess_qt_api ()
45
+
46
+ # backward compatibility
47
+ USING_PYSIDE = QT_API == 'pyside'
48
+
49
+ def _import_module (module_name ):
50
+ m = __import__ (_root_module , globals (), locals (), [module_name ], 0 )
51
+ return getattr (m , module_name )
52
+
53
+ _root_modules = {
54
+ 'pyside' : 'PySide' ,
55
+ 'pyqt4' : 'PyQt4' ,
56
+ 'pyqt5' : 'PyQt5' ,
57
+ }
58
+ _root_module = _root_modules [QT_API ]
59
+
60
+ QtCore = _import_module ('QtCore' )
61
+ QtGui = _import_module ('QtGui' )
62
+ QtTest = _import_module ('QtTest' )
63
+ Qt = QtCore .Qt
64
+ QEvent = QtCore .QEvent
65
+
66
+ if QT_API == 'pyside' :
37
67
Signal = QtCore .Signal
38
68
Slot = QtCore .Slot
39
69
Property = QtCore .Property
40
- else :
41
- def _import_module (moduleName ):
42
- pyside = __import__ ('PyQt4' , globals (), locals (), [moduleName ], 0 )
43
- return getattr (pyside , moduleName )
44
-
70
+ QApplication = QtGui .QApplication
71
+ QWidget = QtGui .QWidget
72
+
73
+ elif QT_API in ('pyqt4' , 'pyqt5' ):
45
74
Signal = QtCore .pyqtSignal
46
75
Slot = QtCore .pyqtSlot
47
76
Property = QtCore .pyqtProperty
48
-
49
-
50
- QtGui = _import_module ('QtGui' )
51
- QtTest = _import_module ('QtTest' )
52
- Qt = QtCore .Qt
53
- QEvent = QtCore .QEvent
54
-
77
+
78
+ if QT_API == 'pyqt5' :
79
+ _QtWidgets = _import_module ('QtWidgets' )
80
+ QApplication = _QtWidgets .QApplication
81
+ QWidget = _QtWidgets .QWidget
82
+ else :
83
+ QApplication = QtGui .QApplication
84
+ QWidget = QtGui .QWidget
85
+
55
86
else : # pragma: no cover
56
87
USING_PYSIDE = True
57
88
@@ -77,3 +108,6 @@ def __getattr__(cls, name):
77
108
QtTest = Mock ()
78
109
Qt = Mock ()
79
110
QEvent = Mock ()
111
+ QApplication = Mock ()
112
+ QWidget = Mock ()
113
+ QT_API = '<none>'
0 commit comments