首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在QCheckBox中添加级联菜单

如何在QCheckBox中添加级联菜单
EN

Stack Overflow用户
提问于 2021-01-05 07:35:37
回答 1查看 86关注 0票数 0

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

代码语言:javascript
复制
 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个选择框,

但是现在,对于每个框,我要添加级联菜单,就像下面的图片所示

EN

回答 1

Stack Overflow用户

发布于 2021-01-07 14:42:01

假设你想要的东西已经在eyllanesc's example中呈现了,但是带有复选框,下面是这个代码的一个版本,带有复选框,没有动画:

代码语言:javascript
复制
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_()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65571292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档