2014-01-22 57 views
0

我試圖使用自定義窗口小部件添加到qtdesginer下面的代碼錯誤添加matplotlib工具到PyQt4的

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) 

但我只給我

類型錯誤的錯誤:「PySide.QtGui.QWidget .setSizePolicy」調用錯誤的參數類型: PySide.QtGui.QWidget.setSizePolicy(方針,政策) 支持的簽名: PySide.QtGui.QWidget.setSizePolicy(PySide.QtGui.QSizePolicy) PySide.QtGui.QWidget.setSizePolicy( PySide.QtGui.QSizePolicy.Policy,PySide.QtGui。 QSizePolicy.Policy)

我並不確切知道是什麼原因造成的錯誤,因爲我bascially遵循了這一部分http://packtlib.packtpub.com/library/9781847197900/ch06lvl1sec04

任何建議將是很好的,因爲我是新來這個Qt設計。

+0

您不能混用' PyQt4'和'PySides' – tacaswell

回答

0

我已經在我的自定義matplotlib Widgets中繼承了FigureCanvas的super(...).__init__(),而不是BaseClass.__init__(self)方法。你的widget工作對我來說有一些小的變化:

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() 

而且......我@tcaswell同意,你應該挑任PyQt4的或PySide,避免導入這兩種:)