From 16c4e2929f3a8da7b7d83c735e799ca60a66bb6c Mon Sep 17 00:00:00 2001 From: Luke Campagnola <> Date: Mon, 18 Jun 2012 17:47:56 -0400 Subject: [PATCH 1/4] fix for ScatterPlotItem / pyside --- graphicsItems/ScatterPlotItem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphicsItems/ScatterPlotItem.py b/graphicsItems/ScatterPlotItem.py index e73653b7..53075090 100644 --- a/graphicsItems/ScatterPlotItem.py +++ b/graphicsItems/ScatterPlotItem.py @@ -36,7 +36,7 @@ for k, c in coords.items(): def makeSymbolPixmap(size, pen, brush, symbol): ## Render a spot with the given parameters to a pixmap penPxWidth = max(np.ceil(pen.width()), 1) - image = QtGui.QImage(size+penPxWidth, size+penPxWidth, QtGui.QImage.Format_ARGB32_Premultiplied) + image = QtGui.QImage(int(size+penPxWidth), int(size+penPxWidth), QtGui.QImage.Format_ARGB32_Premultiplied) image.fill(0) p = QtGui.QPainter(image) p.setRenderHint(p.Antialiasing) From a90d00a536cc58eae91ef84ead97e99e29786b24 Mon Sep 17 00:00:00 2001 From: Luke Campagnola <> Date: Mon, 18 Jun 2012 17:48:33 -0400 Subject: [PATCH 2/4] metaarray import fix for flowchart/Filters --- flowchart/library/Filters.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/flowchart/library/Filters.py b/flowchart/library/Filters.py index 051afb91..40c55f05 100644 --- a/flowchart/library/Filters.py +++ b/flowchart/library/Filters.py @@ -8,11 +8,7 @@ from . import functions from .common import * import numpy as np -try: - import metaarray - HAVE_METAARRAY = True -except: - HAVE_METAARRAY = False +import pyqtgraph.metaarray as metaarray class Downsample(CtrlNode): @@ -145,11 +141,11 @@ class Derivative(CtrlNode): nodeName = 'DerivativeFilter' def processData(self, data): - if HAVE_METAARRAY and (hasattr(data, 'implements') and data.implements('MetaArray')): + if hasattr(data, 'implements') and data.implements('MetaArray'): info = data.infoCopy() if 'values' in info[0]: info[0]['values'] = info[0]['values'][:-1] - return MetaArray(data[1:] - data[:-1], info=info) + return metaarray.MetaArray(data[1:] - data[:-1], info=info) else: return data[1:] - data[:-1] From 3f486d9a65c0e06ee2f124c1bbe89ab4ee214293 Mon Sep 17 00:00:00 2001 From: Luke Campagnola <> Date: Mon, 18 Jun 2012 19:40:15 -0400 Subject: [PATCH 3/4] minor code cleanup bugfixes for pyside-specific issues --- GraphicsScene/GraphicsScene.py | 11 +++++++---- __init__.py | 5 ++++- graphicsItems/GraphicsItem.py | 19 +++++++++++++------ graphicsItems/GraphicsObject.py | 11 +---------- graphicsItems/GraphicsWidget.py | 14 +++++--------- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/GraphicsScene/GraphicsScene.py b/GraphicsScene/GraphicsScene.py index c80e97e5..73867059 100644 --- a/GraphicsScene/GraphicsScene.py +++ b/GraphicsScene/GraphicsScene.py @@ -13,10 +13,13 @@ from .mouseEvents import * import pyqtgraph.debug as debug from . import exportDialog -try: - import sip - HAVE_SIP = True -except: +if hasattr(QtCore, 'PYQT_VERSION'): + try: + import sip + HAVE_SIP = True + except ImportError: + HAVE_SIP = False +else: HAVE_SIP = False diff --git a/__init__.py b/__init__.py index 4c768a54..51a0fe5c 100644 --- a/__init__.py +++ b/__init__.py @@ -193,7 +193,10 @@ show = image ## for backward compatibility def mkQApp(): global QAPP - if QtGui.QApplication.instance() is None: + inst = QtGui.QApplication.instance() + if inst is None: QAPP = QtGui.QApplication([]) + else: + QAPP = inst return QAPP diff --git a/graphicsItems/GraphicsItem.py b/graphicsItems/GraphicsItem.py index 0d0491f1..ba4858cf 100644 --- a/graphicsItems/GraphicsItem.py +++ b/graphicsItems/GraphicsItem.py @@ -76,7 +76,7 @@ class GraphicsItem(object): if view is None: return None viewportTransform = view.viewportTransform() - dt = QtGui.QGraphicsObject.deviceTransform(self, viewportTransform) + dt = self._qtBaseClass.deviceTransform(self, viewportTransform) #xmag = abs(dt.m11())+abs(dt.m12()) #ymag = abs(dt.m21())+abs(dt.m22()) @@ -274,19 +274,26 @@ class GraphicsItem(object): return vt.mapRect(obj) def pos(self): - return Point(QtGui.QGraphicsObject.pos(self)) + return Point(self._qtBaseClass.pos(self)) def viewPos(self): return self.mapToView(self.mapFromParent(self.pos())) def parentItem(self): ## PyQt bug -- some items are returned incorrectly. - return GraphicsScene.translateGraphicsItem(QtGui.QGraphicsObject.parentItem(self)) + return GraphicsScene.translateGraphicsItem(self._qtBaseClass.parentItem(self)) + def setParentItem(self, parent): + ## Workaround for Qt bug: https://bugreports.qt-project.org/browse/QTBUG-18616 + if parent is not None: + pscene = parent.scene() + if pscene is not None and self.scene() is not pscene: + pscene.addItem(self) + return self._qtBaseClass.setParentItem(self, parent) def childItems(self): ## PyQt bug -- some child items are returned incorrectly. - return list(map(GraphicsScene.translateGraphicsItem, QtGui.QGraphicsObject.childItems(self))) + return list(map(GraphicsScene.translateGraphicsItem, self._qtBaseClass.childItems(self))) def sceneTransform(self): @@ -296,7 +303,7 @@ class GraphicsItem(object): if self.scene() is None: return self.transform() else: - return QtGui.QGraphicsObject.sceneTransform(self) + return self._qtBaseClass.sceneTransform(self) def transformAngle(self, relativeItem=None): @@ -315,7 +322,7 @@ class GraphicsItem(object): #def itemChange(self, change, value): - #ret = QtGui.QGraphicsObject.itemChange(self, change, value) + #ret = self._qtBaseClass.itemChange(self, change, value) #if change == self.ItemParentHasChanged or change == self.ItemSceneHasChanged: #print "Item scene changed:", self #self.setChildScene(self) ## This is bizarre. diff --git a/graphicsItems/GraphicsObject.py b/graphicsItems/GraphicsObject.py index 91354cfb..f893d8dc 100644 --- a/graphicsItems/GraphicsObject.py +++ b/graphicsItems/GraphicsObject.py @@ -8,6 +8,7 @@ class GraphicsObject(GraphicsItem, QtGui.QGraphicsObject): Extension of QGraphicsObject with some useful methods (provided by :class:`GraphicsItem `) """ + _qtBaseClass = QtGui.QGraphicsObject def __init__(self, *args): QtGui.QGraphicsObject.__init__(self, *args) GraphicsItem.__init__(self) @@ -17,13 +18,3 @@ class GraphicsObject(GraphicsItem, QtGui.QGraphicsObject): if change in [self.ItemParentHasChanged, self.ItemSceneHasChanged]: self._updateView() return ret - - - - def setParentItem(self, parent): - ## Workaround for Qt bug: https://bugreports.qt-project.org/browse/QTBUG-18616 - if parent is not None: - pscene = parent.scene() - if pscene is not None and self.scene() is not pscene: - pscene.addItem(self) - return QtGui.QGraphicsObject.setParentItem(self, parent) diff --git a/graphicsItems/GraphicsWidget.py b/graphicsItems/GraphicsWidget.py index c131a54a..8f28d208 100644 --- a/graphicsItems/GraphicsWidget.py +++ b/graphicsItems/GraphicsWidget.py @@ -5,6 +5,8 @@ from .GraphicsItem import GraphicsItem __all__ = ['GraphicsWidget'] class GraphicsWidget(GraphicsItem, QtGui.QGraphicsWidget): + + _qtBaseClass = QtGui.QGraphicsWidget def __init__(self, *args, **kargs): """ **Bases:** :class:`GraphicsItem `, :class:`QtGui.QGraphicsWidget` @@ -14,7 +16,9 @@ class GraphicsWidget(GraphicsItem, QtGui.QGraphicsWidget): """ QtGui.QGraphicsWidget.__init__(self, *args, **kargs) GraphicsItem.__init__(self) - GraphicsScene.registerObject(self) ## workaround for pyqt bug in graphicsscene.items() + + ## done by GraphicsItem init + #GraphicsScene.registerObject(self) ## workaround for pyqt bug in graphicsscene.items() ## Removed because this causes segmentation faults. Don't know why. # def itemChange(self, change, value): @@ -52,11 +56,3 @@ class GraphicsWidget(GraphicsItem, QtGui.QGraphicsWidget): return p - - def setParentItem(self, parent): - ## Workaround for Qt bug: https://bugreports.qt-project.org/browse/QTBUG-18616 - if parent is not None: - pscene = parent.scene() - if pscene is not None and self.scene() is not pscene: - pscene.addItem(self) - return QtGui.QGraphicsObject.setParentItem(self, parent) From acb3230b78ea7c766517703cf81320520aa45de1 Mon Sep 17 00:00:00 2001 From: Luke Campagnola <> Date: Mon, 18 Jun 2012 19:51:18 -0400 Subject: [PATCH 4/4] bugfix -- GraphicsItem automatically determines qt base class. --- graphicsItems/GraphicsItem.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/graphicsItems/GraphicsItem.py b/graphicsItems/GraphicsItem.py index ba4858cf..67fef456 100644 --- a/graphicsItems/GraphicsItem.py +++ b/graphicsItems/GraphicsItem.py @@ -15,11 +15,20 @@ class GraphicsItem(object): The GraphicsView system places a lot of emphasis on the notion that the graphics within the scene should be device independent--you should be able to take the same graphics and display them on screens of different resolutions, printers, export to SVG, etc. This is nice in principle, but causes me a lot of headache in practice. It means that I have to circumvent all the device-independent expectations any time I want to operate in pixel coordinates rather than arbitrary scene coordinates. A lot of the code in GraphicsItem is devoted to this task--keeping track of view widgets and device transforms, computing the size and shape of a pixel in local item coordinates, etc. Note that in item coordinates, a pixel does not have to be square or even rectangular, so just asking how to increase a bounding rect by 2px can be a rather complex task. """ def __init__(self, register=True): + if not hasattr(self, '_qtBaseClass'): + for b in self.__class__.__bases__: + if issubclass(b, QtGui.QGraphicsItem): + self.__class__._qtBaseClass = b + break + if not hasattr(self, '_qtBaseClass'): + raise Exception('Could not determine Qt base class for GraphicsItem: %s' % str(self)) + self._viewWidget = None self._viewBox = None self._connectedView = None if register: GraphicsScene.registerObject(self) ## workaround for pyqt bug in graphicsscene.items() + def getViewWidget(self): """