3

我正在创建一个也使用 MatPlotLib 的简单 PySide 应用程序。但是,当我将图形添加到 aQFrame时,图形不会自动适合框架:

不会自动拉伸的绘图

我的图表是使用以下代码创建的:

class GraphView(gui.QWidget):
    def __init__(self, name, title, graphTitle, parent = None):
        super(GraphView, self).__init__(parent)

        self.name = name
        self.graphTitle = graphTitle

        self.dpi = 100
        self.fig = Figure((5.0, 3.0), dpi = self.dpi, facecolor = (1,1,1), edgecolor = (0,0,0))
        self.axes = self.fig.add_subplot(111)
        self.canvas = FigureCanvas(self.fig)

        self.Title = gui.QLabel(self)
        self.Title.setText(title)

        self.layout = gui.QVBoxLayout()
        self.layout.addStretch(1)
        self.layout.addWidget(self.Title)
        self.layout.addWidget(self.canvas)
        self.setLayout(self.layout)

    def UpdateGraph(self, data, title = None):
        self.axes.clear()
        self.axes.plot(data)
        if title != None:
            self.axes.set_title(title)

        self.canvas.draw()

它被添加到主 Widget 中,如下所示:

# Create individual Widget/Frame (fftFrame)
        fftFrame = gui.QFrame(self)
        fftFrame.setFrameShape(gui.QFrame.StyledPanel)
        self.FFTGraph = GraphView('fftFrame', 'FFT Transform:', 'FFT Transform of Signal', fftFrame)
4

1 回答 1

5

这是一个工作代码示例,向您展示如何使其工作。我首先认为这是因为您添加到布局中的拉伸,这将占用其他小部件周围的额外空间。但是当我删除它时,它仍然不会调整大小。“简单”的解决方案是添加一个 resizeEvent,它可以让您定义 GraphView 小部件的大小。在这种情况下,我只是将其几何形状设置为 QFrame 的几何形状,尽管您可能想要添加一些填充并确保为 QFrame 设置合理的最小尺寸。

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

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.fft_frame = FftFrame(self)

        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.fft_frame)
        self.setLayout(self.layout)
        self.setCentralWidget(self.fft_frame)

class FftFrame(QtGui.QFrame):
    def __init__(self, parent=None):
        super(FftFrame, self).__init__(parent)
        self.setFrameShape(QtGui.QFrame.StyledPanel)
        self.parent = parent
        self.graph_view = GraphView('fftFrame', 'FFT Transform:', 'FFT Transform of Signal', self)

    def resizeEvent(self, event):
        self.graph_view.setGeometry(self.rect())


class GraphView(QtGui.QWidget):
    def __init__(self, name, title, graph_title, parent = None):
        super(GraphView, self).__init__(parent)

        self.name = name
        self.graph_title = graph_title

        self.dpi = 100
        self.fig = Figure((5.0, 3.0), dpi = self.dpi, facecolor = (1,1,1), edgecolor = (0,0,0))
        self.axes = self.fig.add_subplot(111)
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setParent(self)

        self.Title = QtGui.QLabel(self)
        self.Title.setText(title)

        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.Title)
        self.layout.addWidget(self.canvas)
        self.layout.setStretchFactor(self.canvas, 1)
        self.setLayout(self.layout)
        self.canvas.show()

    def update_graph(self, data, title = None):
        self.axes.clear()
        self.axes.plot(data)
        if title != None:
            self.axes.set_title(title)

        self.canvas.draw()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()
于 2013-07-29T20:04:02.290 回答