1
1
"""
2
2
Provide a common way to import Qt classes used by pytest-qt in a unique manner,
3
- abstracting API differences between PyQt4, PyQt5 and PySide .
3
+ abstracting API differences between PyQt4, PyQt5, PySide and PySide2 .
4
4
5
5
.. note:: This module is not part of pytest-qt public API, hence its interface
6
6
may change between releases and users should not rely on it.
7
7
8
8
Based on from https://github.com/epage/PythonUtils.
9
9
"""
10
10
11
- from __future__ import with_statement
12
- from __future__ import division
11
+ from __future__ import with_statement , division
13
12
from collections import namedtuple
14
13
import os
15
14
@@ -29,7 +28,7 @@ def _get_qt_api_from_env(self):
29
28
api = os .environ .get ('PYTEST_QT_API' )
30
29
if api is not None :
31
30
api = api .lower ()
32
- if api not in ('pyside' , 'pyqt4' , 'pyqt4v2' , 'pyqt5' ): # pragma: no cover
31
+ if api not in ('pyside' , 'pyside2' , ' pyqt4' , 'pyqt4v2' , 'pyqt5' ): # pragma: no cover
33
32
msg = 'Invalid value for $PYTEST_QT_API: %s'
34
33
raise RuntimeError (msg % api )
35
34
return api
@@ -42,22 +41,27 @@ def _can_import(name):
42
41
except ImportError :
43
42
return False
44
43
45
- if _can_import ('PyQt5' ):
44
+ # Note, not importing only the root namespace because when uninstalling from conda,
45
+ # the namespace can still be there.
46
+ if _can_import ('PyQt5.QtCore' ):
46
47
return 'pyqt5'
47
- elif _can_import ('PySide' ):
48
+ elif _can_import ('PySide.QtCore ' ):
48
49
return 'pyside'
49
- elif _can_import ('PyQt4' ):
50
+ elif _can_import ('PySide2.QtCore' ):
51
+ return 'pyside2'
52
+ elif _can_import ('PyQt4.QtCore' ):
50
53
return 'pyqt4'
51
54
return None
52
55
53
56
def set_qt_api (self , api ):
54
57
self .pytest_qt_api = api or self ._get_qt_api_from_env () or self ._guess_qt_api ()
55
58
if not self .pytest_qt_api : # pragma: no cover
56
- msg = 'pytest-qt requires either PySide, PyQt4 or PyQt5 to be installed'
59
+ msg = 'pytest-qt requires either PySide, PySide2, PyQt4 or PyQt5 to be installed'
57
60
raise RuntimeError (msg )
58
61
59
62
_root_modules = {
60
63
'pyside' : 'PySide' ,
64
+ 'pyside2' : 'PySide2' ,
61
65
'pyqt4' : 'PyQt4' ,
62
66
'pyqt4v2' : 'PyQt4' ,
63
67
'pyqt5' : 'PyQt5' ,
@@ -100,21 +104,35 @@ def _import_module(module_name):
100
104
self .qInstallMsgHandler = None
101
105
self .qInstallMessageHandler = None
102
106
103
- if self .pytest_qt_api == 'pyside' :
107
+ if self .pytest_qt_api in ( 'pyside' , 'pyside2' ) :
104
108
self .Signal = QtCore .Signal
105
109
self .Slot = QtCore .Slot
106
110
self .Property = QtCore .Property
107
111
self .QApplication = QtGui .QApplication
108
112
self .QWidget = QtGui .QWidget
109
113
self .QStringListModel = QtGui .QStringListModel
110
- self .qInstallMsgHandler = QtCore .qInstallMsgHandler
111
114
112
115
self .QStandardItem = QtGui .QStandardItem
113
116
self .QStandardItemModel = QtGui .QStandardItemModel
114
- self .QStringListModel = QtGui .QStringListModel
115
- self .QSortFilterProxyModel = QtGui .QSortFilterProxyModel
116
117
self .QAbstractListModel = QtCore .QAbstractListModel
117
118
self .QAbstractTableModel = QtCore .QAbstractTableModel
119
+ self .QStringListModel = QtGui .QStringListModel
120
+
121
+ if self .pytest_qt_api == 'pyside2' :
122
+ _QtWidgets = _import_module ('QtWidgets' )
123
+ self .QApplication = _QtWidgets .QApplication
124
+ self .QWidget = _QtWidgets .QWidget
125
+ self .QLineEdit = _QtWidgets .QLineEdit
126
+ self .qInstallMessageHandler = QtCore .qInstallMessageHandler
127
+
128
+ self .QSortFilterProxyModel = QtCore .QSortFilterProxyModel
129
+ else :
130
+ self .QApplication = QtGui .QApplication
131
+ self .QWidget = QtGui .QWidget
132
+ self .QLineEdit = QtGui .QLineEdit
133
+ self .qInstallMsgHandler = QtCore .qInstallMsgHandler
134
+
135
+ self .QSortFilterProxyModel = QtGui .QSortFilterProxyModel
118
136
119
137
def extract_from_variant (variant ):
120
138
"""PySide does not expose QVariant API"""
@@ -180,9 +198,16 @@ def make_variant(value=None):
180
198
self .make_variant = make_variant
181
199
182
200
def get_versions (self ):
183
- if self .pytest_qt_api == 'pyside' :
184
- import PySide
185
- return VersionTuple ('PySide' , PySide .__version__ , self .QtCore .qVersion (),
201
+ if self .pytest_qt_api in ('pyside' , 'pyside2' ):
202
+ qt_api_name = 'PySide2' if self .pytest_qt_api == 'pyside2' else 'PySide'
203
+ if self .pytest_qt_api == 'pyside2' :
204
+ import PySide2
205
+ version = PySide2 .__version__
206
+ else :
207
+ import PySide
208
+ version = PySide .__version__
209
+
210
+ return VersionTuple (qt_api_name , version , self .QtCore .qVersion (),
186
211
self .QtCore .__version__ )
187
212
else :
188
213
qt_api_name = 'PyQt5' if self .pytest_qt_api == 'pyqt5' else 'PyQt4'
0 commit comments