Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

reorder qt support in kernel

import priority:

1. ask matplotlib (if imported) - if PyQt4 specified, use API v1
2. ask QT_API - if pyqt specified, use API v2
3. if nothing specified, try PyQt4 v1, fallback on PySide.
  • Loading branch information...
commit 6bc24fa32394bb740edaaa6199b9f70cbfe2c37f 1 parent 3207b26
Min RK authored
5 IPython/external/qt.py
... ... @@ -1,4 +1,9 @@
1 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 9 import os
71 IPython/external/qt_for_kernel.py
... ... @@ -1,12 +1,75 @@
1 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 +Priority:
  6 +
  7 +if matplotlib has been imported:
  8 + # get here with pylab=qt
  9 + if matplotlib doesn't support v2 (<= 1.0.1):
  10 + use PyQt4 @v1
  11 + else:
  12 + ask matplotlib which Qt it's using
  13 + if it said PyQt:
  14 + use PyQt4 @v1
  15 + elif it said PySide:
  16 + use PySide
  17 +
  18 +if matplotlib had nothing to say, or matplotlib not imported:
  19 + # get here with gui=qt, or if matplotlib didn't tell us anything
  20 + ask ETS' QT_API env variable
  21 +
  22 + if QT_API not set:
  23 + # this is the *default* path - no information was given
  24 + try:
  25 + PyQt @v1
  26 + except:
  27 + fallback on PySide
  28 + else:
  29 + use PyQt @v2 or PySide, depending on QT_API
  30 + because ETS doesn't work with v1.
2 31 """
3 32
  33 +import os
4 34 import sys
5 35
6   -# Older versions of matplotlib do not support PyQt4 v2 APIs or PySide, so we
7   -# cannot go through the preferred mechanism.
8 36 matplotlib = sys.modules.get('matplotlib')
9   -if matplotlib and matplotlib.__version__ <= '1.0.1':
  37 +if matplotlib:
  38 + # ask matplotlib first (get here with pylab=qt)
  39 + if matplotlib.__version__ <= '1.0.1':
  40 + # 1.0.1 doesn't support pyside or v2, so force PyQt @v1
  41 + mod = 'PyQt4'
  42 + else:
  43 + # this rc option has been proposed, but is yet not in matplotlib master
  44 + # as of writing.
  45 + mod = matplotlib.rcParams.get('backend.qt4', None)
  46 +else:
  47 + # get here with `gui=qt`
  48 + mod = None
  49 +
  50 +if mod is None:
  51 + # matplotlib not imported or had nothing to say.
  52 + # ask QT_API ETS variable
  53 + QT_API = os.environ.get('QT_API', None)
  54 + if QT_API is None:
  55 + try:
  56 + # default to unconfigured PyQt4
  57 + from PyQt4 import QtCore, QtGui
  58 + except ImportError:
  59 + # fallback on PySide
  60 + try:
  61 + from PySide import QtCore, QtGui
  62 + except ImportError:
  63 + raise ImportError('Cannot import PySide or PyQt4')
  64 + else:
  65 + # QT_API specified, use PySide or PyQt+v2 API from external.qt
  66 + # this means ETS is likely to be used, which requires v2
  67 + from IPython.external.qt import QtCore, QtGui
  68 +
  69 +elif mod.lower() == 'pyqt4':
  70 + # import PyQt4 unconfigured
10 71 from PyQt4 import QtCore, QtGui
  72 +elif mod.lower() == 'pyside':
  73 + from PySide import QtCore, QtGui
11 74 else:
12   - from IPython.external.qt import QtCore, QtGui
  75 + raise ImportError("unhandled value for backend.qt4 from matplotlib: %r"%mod)
40 docs/source/interactive/reference.txt
@@ -1256,6 +1256,46 @@ process pending events at critical points.
1256 1256 Finally, we also have a number of examples in our source directory
1257 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 +If you launch IPython in pylab mode with ``ipython pylab=qt``, then IPython
  1273 +will ask matplotlib which Qt library to use, via the 'backend.qt4' rcParam.
  1274 +If matplotlib is version 1.0.1 or older, then IPython will always use PyQt4
  1275 +without setting the v2 APIs.
  1276 +
  1277 +If you just integrate the Qt event loop with ``ipython gui=qt``, then IPython
  1278 +has a few more possibilities. The default will be to import PyQt4 without
  1279 +configuration of the APIs, thus matching what most applications would expect.
  1280 +It will fall back of PySide if PyQt4 is unavailable.
  1281 +
  1282 +If specified, IPython will respect the environment variable ``QT_API`` used
  1283 +by ETS. ETS 4.0 also works with both PyQt4 and PySide, but it requires
  1284 +PyQt4 to use the v2 API. So if ``QT_API=pyside`` PySide will be used,
  1285 +and if ``QT_API=pyqt`` then PyQt4 will be used with the v2 API for
  1286 +QString and QVariant, so ETS codes like MayaVi will also work with IPython.
  1287 +
  1288 +.. warning::
  1289 +
  1290 + Note that this means for ETS 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 1299 .. _matplotlib_support:
1260 1300
1261 1301 Plotting with matplotlib

0 comments on commit 6bc24fa

Please sign in to comment.
Something went wrong with that request. Please try again.