Skip to content

Add support for PyQt5. - #5458

Merged
takluyver merged 1 commit into
ipython:masterfrom
pwuertz:qt5
May 2, 2014
Merged

takluyver merged 1 commit into
ipython:masterfrom
pwuertz:qt5

Conversation

@pwuertz

@pwuertz pwuertz commented Mar 28, 2014

Copy link
Copy Markdown
Contributor

This PR adds the new QT_API_PYQT5 to IPython/external/qt_loaders.py and allows to embed the IPython console in Qt5 Applications. In case of PyQt5, the loader returns a union of PyQt5.QtGui and PyQt5.QtWidgets acting as a Qt4-QtGui compatibility module.

One small fix to InProcessChannel.__init__ was necessary though. For some reason, when constructing QtInProcessShellChannel, the QtCore.QObject.__init__ initializer from SuperQObject indirectly calls the InProcessChannel.__init__ function, which doesn't have a default constructor and thus fails. I assume this has something to do with the metaclass/multiple inheritance approach IPython uses on QObject. Adding a default value for client in InProcessChannel.__init__ solved the problem for me.

@tacaswell

Copy link
Copy Markdown
Contributor

Does this require mpl to play nice with QT5 as well?

@pwuertz

pwuertz commented Mar 28, 2014

Copy link
Copy Markdown
Contributor Author

Oh, I haven't checked mpl yet, I just needed some interactive shell in a Qt5 application. But true, something similar should work for mpl as well (checking for existing Qt imports and join QtGui+QtWidgets if PyQt5 is loaded).

@pwuertz

pwuertz commented Mar 28, 2014

Copy link
Copy Markdown
Contributor Author

Cross-link to PR in matplotlib: matplotlib/matplotlib#2471

@pwuertz

pwuertz commented Mar 28, 2014

Copy link
Copy Markdown
Contributor Author

Interresting. The behaviour of PyQt5.QObject.__init__() calling the __init__ functions of mixin classes is indeed a PyQt5 feature (http://pyqt.sourceforge.net/Docs/PyQt5/multiinheritance.html#ref-cooperative-multiinheritance). Although these init functions are called 2x at the moment, this doesn't seem to cause any trouble.

@tacaswell

Copy link
Copy Markdown
Contributor

gah, sorry for botching the github-foo

@takluyver takluyver added this to the 3.0 milestone Mar 28, 2014
@takluyver

Copy link
Copy Markdown
Member

Thanks. We're coming up to the 2.0 release, so we'll probably leave this until after the release, to avoid breaking things accidentally.

@pwuertz

pwuertz commented Mar 28, 2014

Copy link
Copy Markdown
Contributor Author

@takluyver Then it's a good thing that the PR also re-fixes the problem of accidentally mixing different Qt apis since IPython doesn't detect PyQt5 at the moment ;)
No seriously, I understand if you don't want to pull in last-minute changes for a major release. Let me know if there is anything I should add or change.

@takluyver

Copy link
Copy Markdown
Member

It might be a candidate for a backport to 2.1 - it's kind of in the grey area between bugfix and feature. Thanks for being understanding.

There's probably a couple more doc changes to do, e.g. module level docstrings. Nothing else I can see at the moment.

Comment thread IPython/external/qt_loaders.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to create a new class for this? Can we just instantiate types.ModuleType and update the object's __dict__?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recently had an error when trying to update a __dict__ attribute in python3 since it was of type mappingproxy instead of dict, so I assumed this approach was discouraged. This however doesn't seem to be the case here, so I'll modify the PR according to your suggestion.

@takluyver

Copy link
Copy Markdown
Member

Thanks, I think this looks good now.

takluyver added a commit that referenced this pull request May 2, 2014
@takluyver
takluyver merged commit 3af85c3 into ipython:master May 2, 2014
@erikhvatum

Copy link
Copy Markdown
Contributor

With PyQt5 installed and PyQt4 not installed, I have to import PyQt5 modules before %gui qt works:

In [2]: %gui qt
ERROR:
Could not load requested Qt binding. Please ensure that
PyQt4 >= 4.7, PyQt5 or PySide >= 1.0.3 is available,
and only one is imported per session.

Currently-imported Qt library:   None
PyQt4 installed:                 False
PyQt5 installed:                 True
PySide >= 1.0.3 installed:       False
Tried to load:                   ['pyqtdefault', 'pyside']

In [3]: from PyQt5 import QtCore, QtGui, QtWidgets, QtOpenGL

In [4]: %gui qt
Out[4]: <PyQt5.QtWidgets.QApplication at 0x7fcf956ae8a0>

I am on commit 3639432.

@jenshnielsen

Copy link
Copy Markdown
Contributor

That seems to work for me.

export QT_API=pyqt5
ipython
%gui qt

%matplotlib and %pylab naturally doesn't work since there is no support in MPL yet.

However the qtconsole doesn't work for me

QT_API=pyqt5 ipython qtconsole
Traceback (most recent call last):
  File "/Users/jhn/Envs/ipython_master/bin/ipython", line 9, in <module>
    load_entry_point('ipython==3.0.0-dev', 'console_scripts', 'ipython')()
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/__init__.py", line 120, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/config/application.py", line 548, in launch_instance
    app.initialize(argv)
  File "<string>", line 2, in initialize
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/config/application.py", line 74, in catch_config_error
    return method(app, *args, **kwargs)
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 319, in initialize
    super(TerminalIPythonApp, self).initialize(argv)
  File "<string>", line 2, in initialize
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/config/application.py", line 74, in catch_config_error
    return method(app, *args, **kwargs)
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/core/application.py", line 378, in initialize
    self.parse_command_line(argv)
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 314, in parse_command_line
    return super(TerminalIPythonApp, self).parse_command_line(argv)
  File "<string>", line 2, in parse_command_line
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/config/application.py", line 74, in catch_config_error
    return method(app, *args, **kwargs)
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/config/application.py", line 460, in parse_command_line
    return self.initialize_subcommand(subc, subargv)
  File "<string>", line 2, in initialize_subcommand
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/config/application.py", line 74, in catch_config_error
    return method(app, *args, **kwargs)
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/config/application.py", line 398, in initialize_subcommand
    self.subapp.initialize(argv)
  File "<string>", line 2, in initialize
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/config/application.py", line 74, in catch_config_error
    return method(app, *args, **kwargs)
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/qt/console/qtconsoleapp.py", line 344, in initialize
    IPythonConsoleApp.initialize(self,argv)
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/consoleapp.py", line 352, in initialize
    self.init_kernel_client()
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/consoleapp.py", line 339, in init_kernel_client
    self.kernel_client.start_channels()
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/qt/kernel_mixins.py", line 208, in start_channels
    super(QtKernelClientMixin, self).start_channels(*args, **kw)
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/kernel/client.py", line 109, in start_channels
    self.shell_channel.start()
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/kernel/client.py", line 153, in shell_channel
    self.context, self.session, url
  File "/Users/jhn/Envs/ipython_master/lib/python2.7/site-packages/IPython/qt/util.py", line 67, in __new__
    QtCore.QObject.__init__(inst)
TypeError: __init__() takes exactly 4 arguments (1 given)

If you suspect this is an IPython bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@scipy.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    c.Application.verbose_crash=True

@pwuertz
pwuertz deleted the qt5 branch May 7, 2014 05:28
@pwuertz

pwuertz commented May 7, 2014

Copy link
Copy Markdown
Contributor Author

Apparently, there is another Mixin-Class without default constructor involved when using qtconsole. I'll have a look at it.

@mfitzp

mfitzp commented Jun 30, 2014

Copy link
Copy Markdown

@jenshnielsen @pwuertz Is there a bug open for the above issue? Having the same problems with PyQt5 at qtconsole myself.

@mfitzp

mfitzp commented Jul 9, 2014

Copy link
Copy Markdown

I've opened a bug report here: #6103 with additional info I've managed to figure out (the error is occurring in the ChannelQObject initialisation).

mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
@juliantaylor

Copy link
Copy Markdown
Contributor

this should probably be backported to 2.4 if you are still planing that. Some distributions are planning to remove qt4 soon (debian maybe in the next cycle).

@minrk minrk modified the milestones: 2.4, 3.0 Nov 11, 2014
minrk added a commit that referenced this pull request Nov 11, 2014
This PR adds the new `QT_API_PYQT5` to `IPython/external/qt_loaders.py ` and allows to embed the IPython console in Qt5 Applications. In case of PyQt5, the loader returns a union of `PyQt5.QtGui` and `PyQt5.QtWidgets` acting as a Qt4-QtGui compatibility module.

One small fix to `InProcessChannel.__init__` was necessary though. For some reason, when constructing `QtInProcessShellChannel`, the `QtCore.QObject.__init__` initializer from `SuperQObject` indirectly calls the `InProcessChannel.__init__` function, which doesn't have a default constructor and thus fails. I assume this has something to do with the metaclass/multiple inheritance approach IPython uses on QObject. Adding a default value for `client` in `InProcessChannel.__init__` solved the problem for me.
@minrk

minrk commented Nov 11, 2014

Copy link
Copy Markdown
Member

Backported to 2.x, thanks.

minrk added a commit that referenced this pull request Nov 12, 2014
This PR adds the new `QT_API_PYQT5` to `IPython/external/qt_loaders.py ` and allows to embed the IPython console in Qt5 Applications. In case of PyQt5, the loader returns a union of `PyQt5.QtGui` and `PyQt5.QtWidgets` acting as a Qt4-QtGui compatibility module.

One small fix to `InProcessChannel.__init__` was necessary though. For some reason, when constructing `QtInProcessShellChannel`, the `QtCore.QObject.__init__` initializer from `SuperQObject` indirectly calls the `InProcessChannel.__init__` function, which doesn't have a default constructor and thus fails. I assume this has something to do with the metaclass/multiple inheritance approach IPython uses on QObject. Adding a default value for `client` in `InProcessChannel.__init__` solved the problem for me.
@minrk minrk modified the milestones: 2.3.1, 2.4 Nov 12, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants