以下是我编写的一些代码(和/或从其他来源改编而来):
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_()
我希望中间的情节占据尽可能多的空间,以便红色背景变得不可见。
如果我排除了设置窗口大小的命令,就会发生这种情况。然而,窗口太小了 - 我需要把它做得更大。
我尝试过使用self.view.setGeometry
,但它似乎没有什么区别。我已经查看了documentation中的可用模块,但无法确定哪些可能会有所帮助。
答案 0 :(得分:2)
如果您想确定显示的图块覆盖窗口内的所有可用空间,我认为没有必要使用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_())
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_())