使小部件尽可能大

Make widget as big as possible

这是我写的一些代码(and/or 从其他来源改编而来):

import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import (QGraphicsView, QGraphicsScene, QMainWindow,
                             QApplication, QWidget, QVBoxLayout, 
                             QDesktopWidget)
from PyQt5.QtGui import QBrush
from PyQt5.QtCore import Qt
import sys

class View(QGraphicsView):

    def __init__(self):
        super(View, self).__init__()

        self.initScene()

    def initScene(self):     

        self.scene = QGraphicsScene()
        self.canvas = Fig()
        self.setBackgroundBrush(QBrush(Qt.red))
        self.canvas.draw()
        self.setScene(self.scene)
        self.scene.addWidget(self.canvas)

class Fig(FigureCanvas):

    def __init__(self, *args,**kwargs):
        self.factor = kwargs.pop("factor", 2)
        FigureCanvas.__init__(self, Figure(), *args,**kwargs)
        self.plot()

    def plot(self):
        self.ax = self.figure.add_subplot(111)
        data = np.random.rand(1000)
        self.ax.plot(data, '-')


class Window(QMainWindow):

    def __init__(self):

        QMainWindow.__init__(self)

        desktop = QDesktopWidget()
        rect = desktop.availableGeometry()
        self.setGeometry(rect.width()/10, rect.height()/10, rect.width()/1.2,
                   rect.height()/1.2)

        self.view = View()
        self.setCentralWidget(self.view)

app = QApplication(sys.argv)
window = Window()
window.show()
app.exec_()

它产生以下 window 作为输出:

我希望中间的情节尽量占据space,这样红色背景就看不见了。

如果我排除设置 window 大小的命令,就会发生这种情况,这确实会发生。然而,window 太小了——我需要把它变大。

我试过使用self.view.setGeometry,但似乎没有什么不同。我查看了 documentation 中的可用模块,但不知道哪个可能有帮助。

如果您想确定显示的情节涵盖了 window 中所有可用的 space,我认为没有必要使用 QGraphicsView,因为 Fig 是一个QWidget可以直接用在setCentralWidget():

class Fig(FigureCanvas):
    def __init__(self, *args,**kwargs):
        self.factor = kwargs.pop("factor", 2)
        FigureCanvas.__init__(self, Figure(), *args,**kwargs)
        self.plot()

    def plot(self):
        self.ax = self.figure.add_subplot(111)
        data = np.random.rand(1000)
        self.ax.plot(data, '-')


class Window(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        desktop = QDesktopWidget()
        rect = desktop.availableGeometry()
        self.setGeometry(rect.width()/10, rect.height()/10, rect.width()/1.2,
                   rect.height()/1.2)

        self.canvas = Fig()
        self.canvas.draw()
        self.setCentralWidget(self.canvas)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

更新:使用 QScrollArea

class Fig(FigureCanvas):
    def __init__(self, *args,**kwargs):
        self.factor = kwargs.pop("factor", 2)
        FigureCanvas.__init__(self, Figure(), *args,**kwargs)
        self.plot()

    def plot(self):
        self.ax = self.figure.add_subplot(111)
        data = np.random.rand(1000)
        self.ax.plot(data, '-')

    def showEvent(self, event):
        self.setFixedSize(self.size())
        FigureCanvas.showEvent(self, event)


class Window(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        desktop = QDesktopWidget()
        rect = desktop.availableGeometry()
        self.setGeometry(rect.width()/10, rect.height()/10, rect.width()/1.2,
                   rect.height()/1.2)

        self.canvas = Fig()
        self.canvas.draw()

        scrollArea = QScrollArea()
        scrollArea.setWidgetResizable(True)
        scrollArea.setWidget(self.canvas)
        self.setCentralWidget(scrollArea)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())