Show More
@@ -1,4 +1,9 b'' | |||||
1 | """ A Qt API selector that can be used to switch between PyQt and PySide. |
|
1 | """ A Qt API selector that can be used to switch between PyQt and PySide. | |
|
2 | ||||
|
3 | This uses the ETS 4.0 selection pattern of: | |||
|
4 | PySide first, PyQt with API v2. second. | |||
|
5 | ||||
|
6 | Do not use this if you need PyQt with the old QString/QVariant API. | |||
2 | """ |
|
7 | """ | |
3 |
|
8 | |||
4 | import os |
|
9 | import os |
@@ -1,12 +1,82 b'' | |||||
1 | """ Import Qt in a manner suitable for an IPython kernel. |
|
1 | """ Import Qt in a manner suitable for an IPython kernel. | |
|
2 | ||||
|
3 | This is the import used for the `gui=qt` or `pylab=qt` initialization. | |||
|
4 | ||||
|
5 | Import Priority: | |||
|
6 | ||||
|
7 | if matplotlib has been imported and doesn't support v2 (<= 1.0.1): | |||
|
8 | use PyQt4 @v1 | |||
|
9 | ||||
|
10 | Next, ask ETS' QT_API env variable | |||
|
11 | ||||
|
12 | if QT_API not set: | |||
|
13 | ask matplotlib via rcParams['backend.qt4'] | |||
|
14 | if it said PyQt: | |||
|
15 | use PyQt4 @v1 | |||
|
16 | elif it said PySide: | |||
|
17 | use PySide | |||
|
18 | ||||
|
19 | else: (matplotlib said nothing) | |||
|
20 | # this is the default path - nobody told us anything | |||
|
21 | try: | |||
|
22 | PyQt @v1 | |||
|
23 | except: | |||
|
24 | fallback on PySide | |||
|
25 | else: | |||
|
26 | use PyQt @v2 or PySide, depending on QT_API | |||
|
27 | because ETS doesn't work with PyQt @v1. | |||
|
28 | ||||
2 | """ |
|
29 | """ | |
3 |
|
30 | |||
|
31 | import os | |||
4 | import sys |
|
32 | import sys | |
5 |
|
33 | |||
6 | # Older versions of matplotlib do not support PyQt4 v2 APIs or PySide, so we |
|
|||
7 | # cannot go through the preferred mechanism. |
|
|||
8 | matplotlib = sys.modules.get('matplotlib') |
|
34 | matplotlib = sys.modules.get('matplotlib') | |
9 | if matplotlib and matplotlib.__version__ <= '1.0.1': |
|
35 | if matplotlib and matplotlib.__version__ <= '1.0.1': | |
|
36 | # 1.0.1 doesn't support pyside or v2, so stick with PyQt @v1, | |||
|
37 | # and ignore everything else | |||
10 | from PyQt4 import QtCore, QtGui |
|
38 | from PyQt4 import QtCore, QtGui | |
11 | else: |
|
39 | else: | |
12 | from IPython.external.qt import QtCore, QtGui |
|
40 | # ask QT_API ETS variable *first* | |
|
41 | QT_API = os.environ.get('QT_API', None) | |||
|
42 | if QT_API is None: | |||
|
43 | # QT_API not set, ask matplotlib if it was imported (e.g. `pylab=qt`) | |||
|
44 | if matplotlib: | |||
|
45 | mpqt = matplotlib.rcParams.get('backend.qt4', None) | |||
|
46 | else: | |||
|
47 | mpqt = None | |||
|
48 | if mpqt is None: | |||
|
49 | # matplotlib not imported or had nothing to say. | |||
|
50 | try: | |||
|
51 | # default to unconfigured PyQt4 | |||
|
52 | from PyQt4 import QtCore, QtGui | |||
|
53 | except ImportError: | |||
|
54 | # fallback on PySide | |||
|
55 | try: | |||
|
56 | from PySide import QtCore, QtGui | |||
|
57 | except ImportError: | |||
|
58 | raise ImportError('Cannot import PySide or PyQt4') | |||
|
59 | elif mpqt.lower() == 'pyqt4': | |||
|
60 | # import PyQt4 unconfigured | |||
|
61 | from PyQt4 import QtCore, QtGui | |||
|
62 | elif mpqt.lower() == 'pyside': | |||
|
63 | from PySide import QtCore, QtGui | |||
|
64 | else: | |||
|
65 | raise ImportError("unhandled value for backend.qt4 from matplotlib: %r"%mpqt) | |||
|
66 | else: | |||
|
67 | # QT_API specified, use PySide or PyQt+v2 API from external.qt | |||
|
68 | # this means ETS is likely to be used, which requires v2 | |||
|
69 | try: | |||
|
70 | from IPython.external.qt import QtCore, QtGui | |||
|
71 | except ValueError as e: | |||
|
72 | if 'API' in str(e): | |||
|
73 | # API mismatch, give more meaningful message | |||
|
74 | raise ImportError(""" | |||
|
75 | Assigning the ETS variable `QT_API=pyqt` implies PyQt's v2 API for | |||
|
76 | QString and QVariant, but PyQt has already been imported | |||
|
77 | with v1 APIs. You must unset QT_API to work with PyQt4 | |||
|
78 | in its default mode. | |||
|
79 | """) | |||
|
80 | else: | |||
|
81 | raise | |||
|
82 |
@@ -1256,6 +1256,46 b' process pending events at critical points.' | |||||
1256 | Finally, we also have a number of examples in our source directory |
|
1256 | Finally, we also have a number of examples in our source directory | |
1257 | :file:`docs/examples/lib` that demonstrate these capabilities. |
|
1257 | :file:`docs/examples/lib` that demonstrate these capabilities. | |
1258 |
|
1258 | |||
|
1259 | PyQt and PySide | |||
|
1260 | --------------- | |||
|
1261 | ||||
|
1262 | .. attempt at explanation of the complete mess that is Qt support | |||
|
1263 | ||||
|
1264 | When you use ``gui=qt`` or ``pylab=qt``, IPython can work with either | |||
|
1265 | PyQt4 or PySide. There are three options for configuration here, because | |||
|
1266 | PyQt4 has two APIs for QString and QVariant - v1, which is the default on | |||
|
1267 | Python 2, and the more natural v2, which is the only API supported by PySide. | |||
|
1268 | v2 is also the default for PyQt4 on Python 3. IPython's code for the QtConsole | |||
|
1269 | uses v2, but you can still use any interface in your code, since the | |||
|
1270 | Qt frontend is in a different process. | |||
|
1271 | ||||
|
1272 | The default will be to import PyQt4 without configuration of the APIs, thus | |||
|
1273 | matching what most applications would expect. It will fall back of PySide if | |||
|
1274 | PyQt4 is unavailable. | |||
|
1275 | ||||
|
1276 | If specified, IPython will respect the environment variable ``QT_API`` used | |||
|
1277 | by ETS. ETS 4.0 also works with both PyQt4 and PySide, but it requires | |||
|
1278 | PyQt4 to use its v2 API. So if ``QT_API=pyside`` PySide will be used, | |||
|
1279 | and if ``QT_API=pyqt`` then PyQt4 will be used *with the v2 API* for | |||
|
1280 | QString and QVariant, so ETS codes like MayaVi will also work with IPython. | |||
|
1281 | ||||
|
1282 | If you launch IPython in pylab mode with ``ipython pylab=qt``, then IPython | |||
|
1283 | will ask matplotlib which Qt library to use (only if QT_API is *not set*), | |||
|
1284 | via the 'backend.qt4' rcParam. | |||
|
1285 | If matplotlib is version 1.0.1 or older, then IPython will always use PyQt4 | |||
|
1286 | without setting the v2 APIs, since neither v2 PyQt nor PySide work. | |||
|
1287 | ||||
|
1288 | .. warning:: | |||
|
1289 | ||||
|
1290 | Note that this means for ETS 4 to work with PyQt4, ``QT_API`` *must* be set to | |||
|
1291 | work with IPython's qt integration, because otherwise PyQt4 will be loaded in | |||
|
1292 | an incompatible mode. | |||
|
1293 | ||||
|
1294 | It also means that you must *not* have ``QT_API`` set if you want to | |||
|
1295 | use ``gui=qt`` with code that requires PyQt4 API v1. | |||
|
1296 | ||||
|
1297 | ||||
|
1298 | ||||
1259 | .. _matplotlib_support: |
|
1299 | .. _matplotlib_support: | |
1260 |
|
1300 | |||
1261 | Plotting with matplotlib |
|
1301 | Plotting with matplotlib |
General Comments 0
You need to be logged in to leave comments.
Login now