Skip to content

Commit 1cbd70c

Browse files
Jovan MitrevskiJovan Mitrevski
authored andcommitted
fix issues with PyQt5, add more usage information
1 parent d5d814a commit 1cbd70c

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,61 @@ In order to use matplotlib plots with QtQuick, do the following:
99
1. In your QML files, instantiate an object with the QML type defined above. Its `objectName` property specifies the name of the object that can be found in the QML engine. It can be retrieved in python with `win.findChild(QtCore.QObject, "spectFigure")`, for example, where `"spectFigure"` is the `objectName` in this case.
1010
1. One can then pass this object as needed. This is the FigureCanvasQtQuickAgg object. The figure can be found in its `figure` property.
1111

12-
The test in the ``tests`` directory gives a simple example of how to use this backend.
12+
The test in the `tests` directory gives a simple example of how to use this backend.
13+
14+
For interactive plots, if you want to use the toolbar, to the `Bridge` class add slots like:
15+
16+
```python
17+
@QtCore.Slot()
18+
def zoom(self, *args):
19+
"""activate zoom tool."""
20+
self.toolbar.zoom(*args)
21+
```
22+
23+
One can also connect to Matplotlib events:
24+
25+
```python
26+
self._figure.canvas.mpl_connect('button_press_event', self.onClick)
27+
self._figure.canvas.mpl_connect('scroll_event', self.onScroll)
28+
```
29+
30+
and provide the callbacks, for example:
31+
32+
```python
33+
def onClick(self, event):
34+
"""
35+
Handle mouse clicks in the three slices:
36+
set the slice to what is clicked.
37+
"""
38+
if event.button == MouseButton.LEFT:
39+
if event.inaxes == self.axeses[0]:
40+
self.x = self._findBin(self.xAxis, event.xdata)
41+
self.y = self._findBin(self.yAxis, event.ydata)
42+
self.setSlice()
43+
elif event.inaxes == self.axeses[1]:
44+
self.x = self._findBin(self.xAxis, event.xdata)
45+
self.z = self._findBin(self.zAxis, event.ydata)
46+
self.setSlice()
47+
elif event.inaxes == self.axeses[2]:
48+
self.y = self._findBin(self.yAxis, event.xdata)
49+
self.z = self._findBin(self.zAxis, event.ydata)
50+
self.setSlice()
51+
52+
def onScroll(self, event):
53+
"""
54+
Handle mouse scroll in the three slices
55+
- scroll the independent variable
56+
"""
57+
if event.inaxes == self.axeses[0]:
58+
self.z += int(event.step)
59+
self.setSlice()
60+
elif event.inaxes == self.axeses[1]:
61+
self.y += int(event.step)
62+
self.setSlice()
63+
elif event.inaxes == self.axeses[2]:
64+
self.x += int(event.step)
65+
self.setSlice()
66+
```
67+
1368

1469
Please let me know if you have any suggestions or better ways I should do this.

matplotlib_backend_qtquick/backend_qtquick.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
from matplotlib.backends.backend_qt5 import (
1212
TimerQT, SPECIAL_KEYS, MODIFIER_KEYS, cursord)
1313
from .qt_compat import (
14-
QtCore, QtGui, QtQuick, QtWidgets)
14+
QtCore, QtGui, QtQuick, QtWidgets,
15+
QT_API, QT_API_PYSIDE2)
1516

1617

1718
class FigureCanvasQtQuick(QtQuick.QQuickPaintedItem, FigureCanvasBase):
@@ -71,6 +72,9 @@ def _update_figure_dpi(self):
7172
def get_dpi_ratio(self):
7273
return self._dpi_ratio
7374

75+
def boundingRect(self):
76+
return QtCore.QRectF(0, 0, self.width(), self.height())
77+
7478
def set_dpi_ratio(self, new_ratio):
7579
# As described in __init__ above, we need to be careful in cases with
7680
# mixed resolution displays if dpi_ratio is changing between painting
@@ -344,8 +348,12 @@ class NavigationToolbar2QtQuick(QtCore.QObject, NavigationToolbar2):
344348

345349
def __init__(self, canvas, parent=None):
346350

347-
QtCore.QObject.__init__(self, parent)
348-
NavigationToolbar2.__init__(self, canvas)
351+
# I think this is needed due to a bug in PySide2
352+
if QT_API == QT_API_PYSIDE2:
353+
QtCore.QObject.__init__(self, parent)
354+
NavigationToolbar2.__init__(self, canvas)
355+
else:
356+
super().__init__(canvas=canvas, parent=parent)
349357

350358
self._message = ""
351359

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="matplotlib_backend_qtquick",
8-
version="0.0.5",
8+
version="0.0.6",
99
author="Jovan Mitrevski",
1010
author_email="j.p.mitrevski@gmail.com",
1111
description="A QtQuick backend for matplotlib",

tests/testBackend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
An integration test to create a plot using the qtquick backend.
33
"""
4-
4+
import sys
55
from pathlib import Path
66
import unittest
77
import numpy as np
@@ -46,7 +46,7 @@ class TestBackend(unittest.TestCase):
4646

4747
def test_basicPlotting(self):
4848
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
49-
app = QtGui.QGuiApplication()
49+
app = QtGui.QGuiApplication(sys.argv)
5050
engine = QtQml.QQmlApplicationEngine()
5151

5252
# instantate the display bridge

0 commit comments

Comments
 (0)