简体   繁体   中英

error in adding matplotlib widget into pyqt4

I was trying to add a custom widget into qtdesginer using following code

from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import PySide
from matplotlib.figure import Figure

class MplCanvas(FigureCanvas):

    def __init__(self):
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)

        FigureCanvas.__init__(self, self.fig)
        FigureCanvas.setSizePolicy(self, QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)


class MplWidget(QtGui.QWidget):
    def __init__(self, parent = None):

        QtGui.QWidget.__init__(self, parent)
        self.canvas = MplCanvas()
        self.vbl = QtGui.QVBoxLayout()
        self.vbl.addWidget(self.canvas)
        self.setLayout(self.vbl)

But i just give me an error of

TypeError: 'PySide.QtGui.QWidget.setSizePolicy' called with wrong argument types: PySide.QtGui.QWidget.setSizePolicy(Policy, Policy) Supported signatures: PySide.QtGui.QWidget.setSizePolicy(PySide.QtGui.QSizePolicy) PySide.QtGui.QWidget.setSizePolicy(PySide.QtGui.QSizePolicy.Policy, PySide.QtGui.QSizePolicy.Policy)

I am not exactly sure what caused the error, since i bascially followed this part http://packtlib.packtpub.com/library/9781847197900/ch06lvl1sec04

Any suggestions would be good,since i am new to this qt designer.

I've had good luck inheriting FigureCanvas with super(...).__init__() in my custom matplotlib Widgets rather than the BaseClass.__init__(self) method. Your widget worked for me with a few minor changes:

class MplCanvas(FigureCanvas):
    def __init__(self):
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        super(MplCanvas, self).__init__(self.fig)
        self.setSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
        self.updateGeometry()

Also... I agree with @tcaswell that you should probably pick either PyQt4 or PySide and avoid importing both :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM