python - pyqtgraph 中窗口的最小化调整了包含对象的大小,但它应该只显示图形的较小范围
问题描述
我目前正在开发一个项目,该项目在 pyqtgraph 中显示矩形对象。当我最小化我的窗口时,包含的对象也将被调整大小,以便所有对象仍然可见。我的问题是我希望对象保持它们的大小,并且只显示整个图表的较小范围。有什么想法可以解决我的问题吗?
原始 - 图片:
最小化 - 图片:
代码片段:
import pyqtgraph as pg
from pyqtgraph import QtCore, QtGui
from PyQt5.QtGui import QFont
data = [(100, 100), (100, 550), (350,100), (350,550)]
# Creating Rectangle objects
class Rect(pg.GraphicsObject):
def __init__(self, data):
pg.GraphicsObject.__init__(self)
self.data = data
self.generatePicture()
def generatePicture(self):
## pre-computing a QPicture object allows paint() to run much more quickly,
self.picture = QtGui.QPicture()
self.p = QtGui.QPainter(self.picture)
self.p.setFont(QFont("times",50))
self.p.setPen(pg.mkPen('b'))
self.p.setBrush(pg.mkBrush('g'))
for index in self.data:
self.p.drawRect(QtCore.QRectF(index[0],index[1],250,450))
self.p.end()
def paint(self, p, *args):
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
return QtCore.QRectF(self.picture.boundingRect())
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
w = pg.GraphicsWindow()
w.setWindowTitle('Rectangle - Display')
v = w.addViewBox()
v.setAspectLocked()
w.setGeometry(100, 100, 1000, 900)
v.addItem(Rect(data))
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
解决方案
一种可能的解决方案是使用 QScrollArea 作为容器小部件,并且 GraphicsWindow 可以具有固定大小,然后仅更改 QScrollArea 的大小。
import pyqtgraph as pg
from pyqtgraph import QtCore, QtGui
data = [(100, 100), (100, 550), (350, 100), (350, 550)]
# Creating Rectangle objects
class Rect(pg.GraphicsObject):
def __init__(self, data):
pg.GraphicsObject.__init__(self)
self.data = data
self.generatePicture()
def generatePicture(self):
## pre-computing a QPicture object allows paint() to run much more quickly,
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setFont(QtGui.QFont("times", 50))
p.setPen(pg.mkPen("b"))
p.setBrush(pg.mkBrush("g"))
for index in self.data:
p.drawRect(QtCore.QRectF(*index, 250, 450))
p.end()
def paint(self, p, *args):
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
return QtCore.QRectF(self.picture.boundingRect())
pg.setConfigOption("background", "w")
pg.setConfigOption("foreground", "k")
w = pg.GraphicsWindow()
w.setWindowTitle("Rectangle - Display")
v = w.addViewBox()
# Disable the zoom implemented by the mouse wheel
v.setMouseEnabled(x=False, y=False)
v.setAspectLocked()
w.setFixedSize(1000, 900)
v.addItem(Rect(data))
scroll_area = QtGui.QScrollArea()
scroll_area.setWidget(w)
scroll_area.setGeometry(100, 100, 1000, 900)
scroll_area.show()
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == "__main__":
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, "PYQT_VERSION"):
QtGui.QApplication.instance().exec_()
推荐阅读
- docker - 如何编写我的 dockerfile 以便将我的 init.sql 文件添加到 docker mysql 容器
- c - GNU makefile 中的“文件过早结束”是什么意思?
- android-intent - 你能告诉我如何获取应用程序活动吗?我想为应用程序编写意图
- date - intck() 给出负值
- reactjs - React Native 显示嵌套数组项
- javascript - React JS嵌套元素都使用相同的onClick事件并且子事件不起作用
- java - 在Java中将变量从一个类传递给另一个类总是给我一个空错误
- jquery - 切换类以重新触发动画
- python - Groupby 计数为一列和 groupby 熊猫中另一列的唯一值的数量
- c++ - 将 somedialog.h 包含到项目的其他部分使 ui_somedialog.h 对编译器不可见