pyqtgraph 中窗口的最小化调整了包含对象的大小,但它应该只显示图形的较小范围
Posted
技术标签:
【中文标题】pyqtgraph 中窗口的最小化调整了包含对象的大小,但它应该只显示图形的较小范围【英文标题】:Minimizing of window in pyqtgraph resizes contained objects, but it should show just a smaller range of the graph 【发布时间】:2021-10-04 21:16:04 【问题描述】:我目前正在做一个项目,该项目在 pyqtgraph 中显示矩形对象。当我最小化我的窗口时,包含的对象也将被调整大小,以便所有对象仍然可见。我的问题是我希望对象保持它们的大小,并且只显示整个图表的较小范围。有什么想法可以解决我的问题吗?
原始 - 图片:
最小化 - 图片:
代码sn-p:
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_()
【问题讨论】:
【参考方案1】:一种可能的解决方案是使用 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_()
【讨论】:
以上是关于pyqtgraph 中窗口的最小化调整了包含对象的大小,但它应该只显示图形的较小范围的主要内容,如果未能解决你的问题,请参考以下文章