Fixes for PySide compatibility

This commit is contained in:
Luke Campagnola 2012-10-26 08:55:53 -04:00
parent bbba3f1f78
commit 450626a3bb
8 changed files with 86 additions and 13 deletions

View File

@ -87,7 +87,17 @@ class ExampleLoader(QtGui.QMainWindow):
self.ui.loadBtn.clicked.connect(self.loadFile) self.ui.loadBtn.clicked.connect(self.loadFile)
self.ui.exampleTree.currentItemChanged.connect(self.showFile) self.ui.exampleTree.currentItemChanged.connect(self.showFile)
self.ui.exampleTree.itemDoubleClicked.connect(self.loadFile) self.ui.exampleTree.itemDoubleClicked.connect(self.loadFile)
self.ui.pyqtCheck.toggled.connect(self.pyqtToggled)
self.ui.pysideCheck.toggled.connect(self.pysideToggled)
def pyqtToggled(self, b):
if b:
self.ui.pysideCheck.setChecked(False)
def pysideToggled(self, b):
if b:
self.ui.pyqtCheck.setChecked(False)
def populateTree(self, root, examples): def populateTree(self, root, examples):
for key, val in examples.items(): for key, val in examples.items():
@ -108,12 +118,19 @@ class ExampleLoader(QtGui.QMainWindow):
def loadFile(self): def loadFile(self):
fn = self.currentFile() fn = self.currentFile()
extra = []
if self.ui.pyqtCheck.isChecked():
extra.append('pyqt')
elif self.ui.pysideCheck.isChecked():
extra.append('pyside')
if fn is None: if fn is None:
return return
if sys.platform.startswith('win'): if sys.platform.startswith('win'):
os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, '"' + fn + '"') os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, '"' + fn + '"', *extra)
else: else:
os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, fn)
os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, fn, *extra)
def showFile(self): def showFile(self):

View File

@ -39,6 +39,24 @@
</column> </column>
</widget> </widget>
</item> </item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCheckBox" name="pyqtCheck">
<property name="text">
<string>Force PyQt</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="pysideCheck">
<property name="text">
<string>Force PySide</string>
</property>
</widget>
</item>
</layout>
</item>
<item> <item>
<widget class="QPushButton" name="loadBtn"> <widget class="QPushButton" name="loadBtn">
<property name="text"> <property name="text">

View File

@ -2,7 +2,7 @@
# Form implementation generated from reading ui file './examples/exampleLoaderTemplate.ui' # Form implementation generated from reading ui file './examples/exampleLoaderTemplate.ui'
# #
# Created: Sun Sep 9 14:41:31 2012 # Created: Fri Oct 26 07:53:55 2012
# by: PyQt4 UI code generator 4.9.1 # by: PyQt4 UI code generator 4.9.1
# #
# WARNING! All changes made in this file will be lost! # WARNING! All changes made in this file will be lost!
@ -35,6 +35,15 @@ class Ui_Form(object):
self.exampleTree.headerItem().setText(0, _fromUtf8("1")) self.exampleTree.headerItem().setText(0, _fromUtf8("1"))
self.exampleTree.header().setVisible(False) self.exampleTree.header().setVisible(False)
self.verticalLayout.addWidget(self.exampleTree) self.verticalLayout.addWidget(self.exampleTree)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.pyqtCheck = QtGui.QCheckBox(self.layoutWidget)
self.pyqtCheck.setObjectName(_fromUtf8("pyqtCheck"))
self.horizontalLayout.addWidget(self.pyqtCheck)
self.pysideCheck = QtGui.QCheckBox(self.layoutWidget)
self.pysideCheck.setObjectName(_fromUtf8("pysideCheck"))
self.horizontalLayout.addWidget(self.pysideCheck)
self.verticalLayout.addLayout(self.horizontalLayout)
self.loadBtn = QtGui.QPushButton(self.layoutWidget) self.loadBtn = QtGui.QPushButton(self.layoutWidget)
self.loadBtn.setObjectName(_fromUtf8("loadBtn")) self.loadBtn.setObjectName(_fromUtf8("loadBtn"))
self.verticalLayout.addWidget(self.loadBtn) self.verticalLayout.addWidget(self.loadBtn)
@ -51,5 +60,7 @@ class Ui_Form(object):
def retranslateUi(self, Form): def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.pyqtCheck.setText(QtGui.QApplication.translate("Form", "Force PyQt", None, QtGui.QApplication.UnicodeUTF8))
self.pysideCheck.setText(QtGui.QApplication.translate("Form", "Force PySide", None, QtGui.QApplication.UnicodeUTF8))
self.loadBtn.setText(QtGui.QApplication.translate("Form", "Load Example", None, QtGui.QApplication.UnicodeUTF8)) self.loadBtn.setText(QtGui.QApplication.translate("Form", "Load Example", None, QtGui.QApplication.UnicodeUTF8))

View File

@ -2,7 +2,7 @@
# Form implementation generated from reading ui file './examples/exampleLoaderTemplate.ui' # Form implementation generated from reading ui file './examples/exampleLoaderTemplate.ui'
# #
# Created: Sun Sep 9 14:41:31 2012 # Created: Fri Oct 26 07:53:57 2012
# by: pyside-uic 0.2.13 running on PySide 1.1.0 # by: pyside-uic 0.2.13 running on PySide 1.1.0
# #
# WARNING! All changes made in this file will be lost! # WARNING! All changes made in this file will be lost!
@ -30,6 +30,15 @@ class Ui_Form(object):
self.exampleTree.headerItem().setText(0, "1") self.exampleTree.headerItem().setText(0, "1")
self.exampleTree.header().setVisible(False) self.exampleTree.header().setVisible(False)
self.verticalLayout.addWidget(self.exampleTree) self.verticalLayout.addWidget(self.exampleTree)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pyqtCheck = QtGui.QCheckBox(self.layoutWidget)
self.pyqtCheck.setObjectName("pyqtCheck")
self.horizontalLayout.addWidget(self.pyqtCheck)
self.pysideCheck = QtGui.QCheckBox(self.layoutWidget)
self.pysideCheck.setObjectName("pysideCheck")
self.horizontalLayout.addWidget(self.pysideCheck)
self.verticalLayout.addLayout(self.horizontalLayout)
self.loadBtn = QtGui.QPushButton(self.layoutWidget) self.loadBtn = QtGui.QPushButton(self.layoutWidget)
self.loadBtn.setObjectName("loadBtn") self.loadBtn.setObjectName("loadBtn")
self.verticalLayout.addWidget(self.loadBtn) self.verticalLayout.addWidget(self.loadBtn)
@ -46,5 +55,7 @@ class Ui_Form(object):
def retranslateUi(self, Form): def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
self.pyqtCheck.setText(QtGui.QApplication.translate("Form", "Force PyQt", None, QtGui.QApplication.UnicodeUTF8))
self.pysideCheck.setText(QtGui.QApplication.translate("Form", "Force PySide", None, QtGui.QApplication.UnicodeUTF8))
self.loadBtn.setText(QtGui.QApplication.translate("Form", "Load Example", None, QtGui.QApplication.UnicodeUTF8)) self.loadBtn.setText(QtGui.QApplication.translate("Form", "Load Example", None, QtGui.QApplication.UnicodeUTF8))

View File

@ -1,3 +1,8 @@
## make this version of pyqtgraph importable before any others ## make this version of pyqtgraph importable before any others
import sys, os import sys, os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))) sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
if 'pyside' in sys.argv: ## should force example to use PySide instead of PyQt
import PySide
elif 'pyqt' in sys.argv:
import PyQt4

View File

@ -22,7 +22,7 @@ SI_PREFIXES_ASCII = 'yzafpnum kMGTPEZY'
from .Qt import QtGui, QtCore from .Qt import QtGui, QtCore, USE_PYSIDE
import numpy as np import numpy as np
import decimal, re import decimal, re
import ctypes import ctypes
@ -846,8 +846,12 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True):
if copy is True and copied is False: if copy is True and copied is False:
imgData = imgData.copy() imgData = imgData.copy()
addr = ctypes.addressof(ctypes.c_char.from_buffer(imgData, 0)) if USE_PYSIDE:
img = QtGui.QImage(addr, imgData.shape[1], imgData.shape[0], imgFormat) ch = ctypes.c_char.from_buffer(imgData, 0)
img = QtGui.QImage(ch, imgData.shape[1], imgData.shape[0], imgFormat)
else:
addr = ctypes.addressof(ctypes.c_char.from_buffer(imgData, 0))
img = QtGui.QImage(addr, imgData.shape[1], imgData.shape[0], imgFormat)
img.data = imgData img.data = imgData
return img return img
#try: #try:
@ -869,13 +873,19 @@ def imageToArray(img, copy=False, transpose=True):
the QImage is collected before the array, there may be trouble). the QImage is collected before the array, there may be trouble).
The array will have shape (width, height, (b,g,r,a)). The array will have shape (width, height, (b,g,r,a)).
""" """
ptr = img.bits()
ptr.setsize(img.byteCount())
fmt = img.format() fmt = img.format()
ptr = img.bits()
if USE_PYSIDE:
arr = np.frombuffer(ptr, dtype=np.ubyte)
else:
ptr.setsize(img.byteCount())
arr = np.asarray(ptr)
if fmt == img.Format_RGB32: if fmt == img.Format_RGB32:
arr = np.asarray(ptr).reshape(img.height(), img.width(), 3) arr = arr.reshape(img.height(), img.width(), 3)
elif fmt == img.Format_ARGB32 or fmt == img.Format_ARGB32_Premultiplied: elif fmt == img.Format_ARGB32 or fmt == img.Format_ARGB32_Premultiplied:
arr = np.asarray(ptr).reshape(img.height(), img.width(), 4) arr = arr.reshape(img.height(), img.width(), 4)
if copy: if copy:
arr = arr.copy() arr = arr.copy()

View File

@ -28,6 +28,7 @@ class LegendItem(GraphicsWidget):
def addItem(self, item, title): def addItem(self, item, title):
""" """
Add a new entry to the legend. Add a new entry to the legend.
=========== ======================================================== =========== ========================================================
Arguments Arguments
item A PlotDataItem from which the line and point style item A PlotDataItem from which the line and point style

View File

@ -1,4 +1,4 @@
from pyqtgraph.Qt import QtGui, QtCore from pyqtgraph.Qt import QtGui, QtCore, USE_PYSIDE
from pyqtgraph.Point import Point from pyqtgraph.Point import Point
import pyqtgraph.functions as fn import pyqtgraph.functions as fn
from .GraphicsItem import GraphicsItem from .GraphicsItem import GraphicsItem
@ -681,7 +681,7 @@ class ScatterPlotItem(GraphicsObject):
p.resetTransform() p.resetTransform()
if self.opts['useCache']: if not USE_PYSIDE and self.opts['useCache']:
p.drawPixmapFragments(self.fragments, atlas) p.drawPixmapFragments(self.fragments, atlas)
else: else:
for i in range(len(self.data)): for i in range(len(self.data)):