如何解决Python PyQt5 - 折叠框 - 将图形添加到默认折叠框会导致图形大小错误
作为我的 PyQt5 应用程序的一部分,我有一个带有许多部分的侧控制面板(即加载按钮、数据操作、数据点比较等)
为了清理外观,并且没有一个非常长的可滚动区域,我试图将每个部分放入自己的可折叠框中。 我已经从帖子 How to create collapsible box in PyQt 中实现了一个可折叠框,它运行良好(感谢 @eyllanesc 的出色实现)。
作为我实现的一部分,我在其中一个框中有一个 figureCanvas
小部件(来自 from matplotlib.backends.backend_qt5agg
),我将在其中根据用户的选择绘制数据。我的问题是,由于图形大小不是有限的(默认情况下框是折叠的,因此图形的空间不可用),我在加载时收到了 ValueError
。
ValueError: figure size must be positive finite not [ 1.76 -0.05]
我了解问题的根源,但想知道是否有办法解决。
我尝试不创建 figureCanvas
除非框被展开,但似乎框的高度是在启动时设置的,除非我最初创建一个空白图,否则框无法正确显示图必需的。例如,如果我有一个单选按钮列表并尝试根据用户选择增加按钮数量(框的大小不会改变并且按钮都被压扁),也会出现这种情况
下面是基于 How to create collapsible box in PyQt 中的代码的 MWE,底部的修改部分显示在 ##
from PyQt5 import QtCore,QtGui,QtWidgets
from matplotlib.figure import figure
from matplotlib.backends.backend_qt5agg import figureCanvas
class CollapsibleBox(QtWidgets.QWidget):
def __init__(self,title="",parent=None):
super(CollapsibleBox,self).__init__(parent)
self.toggle_button = QtWidgets.QToolButton(
text=title,checkable=True,checked=False
)
self.toggle_button.setStyleSheet("QToolButton { border: none; }")
self.toggle_button.setToolButtonStyle(
QtCore.Qt.ToolButtonTextBesideIcon
)
self.toggle_button.setArrowType(QtCore.Qt.RightArrow)
self.toggle_button.pressed.connect(self.on_pressed)
self.toggle_animation = QtCore.QParallelAnimationGroup(self)
self.content_area = QtWidgets.QScrollArea(
maximumHeight=0,minimumHeight=0
)
self.content_area.setSizePolicy(
QtWidgets.QSizePolicy.Expanding,QtWidgets.QSizePolicy.Fixed
)
self.content_area.setFrameShape(QtWidgets.qframe.NoFrame)
lay = QtWidgets.QVBoxLayout(self)
lay.setSpacing(0)
lay.setContentsMargins(0,0)
lay.addWidget(self.toggle_button)
lay.addWidget(self.content_area)
self.toggle_animation.addAnimation(
QtCore.QPropertyAnimation(self,b"minimumHeight")
)
self.toggle_animation.addAnimation(
QtCore.QPropertyAnimation(self,b"maximumHeight")
)
self.toggle_animation.addAnimation(
QtCore.QPropertyAnimation(self.content_area,b"maximumHeight")
)
@QtCore.pyqtSlot()
def on_pressed(self):
checked = self.toggle_button.isChecked()
self.toggle_button.setArrowType(
QtCore.Qt.DownArrow if not checked else QtCore.Qt.RightArrow
)
self.toggle_animation.setDirection(
QtCore.QAbstractAnimation.Forward
if not checked
else QtCore.QAbstractAnimation.Backward
)
self.toggle_animation.start()
def setContentLayout(self,layout):
lay = self.content_area.layout()
del lay
self.content_area.setLayout(layout)
collapsed_height = (
self.sizeHint().height() - self.content_area.maximumHeight()
)
content_height = layout.sizeHint().height()
for i in range(self.toggle_animation.animationCount()):
animation = self.toggle_animation.animationAt(i)
animation.setDuration(500)
animation.setStartValue(collapsed_height)
animation.setEndValue(collapsed_height + content_height)
content_animation = self.toggle_animation.animationAt(
self.toggle_animation.animationCount() - 1
)
content_animation.setDuration(500)
content_animation.setStartValue(0)
content_animation.setEndValue(content_height)
if __name__ == "__main__":
import sys
import random
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QMainWindow()
w.setCentralWidget(QtWidgets.QWidget())
dock = QtWidgets.QDockWidget("Collapsible Demo")
w.addDockWidget(QtCore.Qt.LeftDockWidgetArea,dock)
scroll = QtWidgets.QScrollArea()
dock.setWidget(scroll)
content = QtWidgets.QWidget()
scroll.setWidget(content)
scroll.setWidgetResizable(True)
vlay = QtWidgets.QVBoxLayout(content)
for i in range(2):
Box = CollapsibleBox("Collapsible Box Header-{}".format(i))
vlay.addWidget(Box)
lay = QtWidgets.QVBoxLayout()
for j in range(8):
label = QtWidgets.QLabel("{}".format(j))
color = QtGui.QColor(*[random.randint(0,255) for _ in range(3)])
label.setStyleSheet(
"background-color: {}; color : white;".format(color.name())
)
label.setAlignment(QtCore.Qt.AlignCenter)
lay.addWidget(label)
Box.setContentLayout(lay)
##########################
# Altered from https://stackoverflow.com/questions/52615115/how-to-create-collapsible-Box-in-pyqt
##########################
# ------------------------
# Works if not in a Box
# ------------------------
canvas = figureCanvas(figure())
vlay.addWidget(canvas)
ax = canvas.figure.subplots()
# ------------------------
# Error if placed in a Box
# ------------------------
Box = CollapsibleBox("Collapsible Box Header-figure")
vlay.addWidget(Box)
lay = QtWidgets.QVBoxLayout()
Box.setContentLayout(lay)
canvas = figureCanvas(figure())
lay.addWidget(canvas)
ax = canvas.figure.subplots()
###############
vlay.addStretch()
w.resize(640,480)
w.show()
sys.exit(app.exec_())
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。