【问题标题】:How to add cascading menu to QCheckBox如何将级联菜单添加到 QCheckBox
【发布时间】:2021-01-04 23:35:37
【问题描述】:

我只是想知道如何在 Pyqt 中通过 QCheckBOx 添加级联菜单,以便对于每个选择,我都可以展开并获得更多选项。这是我下面的代码,

 class Example (QWidget):

    def __init__(self, fname):
        self.fname=fname
        super().__init__()
        self.initUI()

    def initUI(self):
        
        self.sheets= list(fname.keys())     
        print(self.sheets)

        self.cb1 = QCheckBox("Hello", self)
        self.cb2 = QCheckBox("You", self)
        self.cb3 = QCheckBox("Are", self)
        self.cb4 = QCheckBox("My", self)
        self.cb5 = QCheckBox("Sunshine", self)
        
                            
    

        self.resize(300,400)
        self.setWindowTitle('QCheckBox')


        formLayout = QFormLayout()
        groupBox = QGroupBox("This Is Group Box")
      
        formLayout.addRow(self.cb1)
        formLayout.addRow(self.cb2)
        formLayout.addRow(self.cb3)
        formLayout.addRow(self.cb4)
        formLayout.addRow(self.cb5)

所以我有 5 个选择框,

但是现在我想为每个盒子添加级联菜单,就像下面的图片所示

【问题讨论】:

  • “添加级联菜单”是指stackoverflow.com/questions/52615115/…吗? (我有点困惑,为什么@eyllanesc 作为答案的作者没有链接到它......?)我不完全理解你对这段代码的修改愿望是什么以及你没有修改它的地方。
  • @ChristianKarcher 我只编辑了当前帖子,但即使对我来说,也不清楚 OP 想要什么,所以我没有寻找可能的解决方案,如果 OP 指出这篇文章是答案然后我将它作为重复关闭,你认为它是重复的吗?
  • @eyllanesc 我认为他想要您的解决方案,但使用复选框而不是工具按钮,...?我会拿你的代码,去掉所有的动画花哨,然后用一个简单的复选框例子来回答。坦率地说,这主要是为了让我更好地理解你的代码:)

标签: python pyqt pyqt5


【解决方案1】:

基于您想要的内容已在eyllanesc's example 中呈现的假设,但带有复选框,这里是此代码的一个版本,带有复选框且没有动画:

from PyQt5 import QtWidgets


class CollapsibleBox(QtWidgets.QWidget):
    def __init__(self, title=""):
        super().__init__()
        self.toggle_button = QtWidgets.QCheckBox(text=title)
        self.toggle_button.clicked.connect(self.on_clicked)

        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, 0, 0)
        lay.addWidget(self.toggle_button)
        lay.addWidget(self.content_area)

    def on_clicked(self):
        # this is a bit hacky, I just expand the boxes to a max. size of 1000, which should fit most needs
        self.content_area.setMaximumHeight(self.toggle_button.isChecked() * 1000)


class MainUI(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        vlay = QtWidgets.QVBoxLayout()
        for i in range(3):
            # add checkboxes and fully collapsed layouts (containing only labels for demo)
            box = CollapsibleBox(f"Collapsible Box {i+1}")
            vlay.addWidget(box)
            lay = QtWidgets.QVBoxLayout()
            # add items to the collapsed layout
            for j in range(5):
                label = QtWidgets.QLabel("demo")
                lay.addWidget(label)
            box.content_area.setLayout(lay)
        vlay.addStretch()
        widget = QtWidgets.QWidget()
        widget.setLayout(vlay)
        self.setCentralWidget(widget)


if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    w = MainUI()
    w.show()
    app.exec_()

【讨论】:

    猜你喜欢
    • 2017-12-18
    • 1970-01-01
    • 2015-08-05
    • 2012-08-22
    • 1970-01-01
    • 2020-09-06
    • 2013-10-19
    • 2011-11-01
    • 2012-02-02
    相关资源
    最近更新 更多