为了账号安全,请及时绑定邮箱和手机立即绑定

使 QGroupBox 可选择和可点击

使 QGroupBox 可选择和可点击

慕尼黑的夜晚无繁华 2022-11-01 14:02:28
我有以下玩具界面:from PyQt5 import QtWidgets, QtGui, QtCoreclass MainWindow(QtWidgets.QMainWindow):    def __init__(self):        super(MainWindow, self).__init__()        w = QtWidgets.QWidget()        layout = QtWidgets.QVBoxLayout()        w.setLayout(layout)        self.setCentralWidget(w)        my_tree = QtWidgets.QTreeWidget()        layout.addWidget(my_tree)        alpha = QtWidgets.QTreeWidgetItem(my_tree, ['Alpha'])        beta = QtWidgets.QTreeWidgetItem(my_tree, ['Beta'])        alpha.addChild(QtWidgets.QTreeWidgetItem(['one']))        alpha.addChild(QtWidgets.QTreeWidgetItem(['two']))        beta.addChild(QtWidgets.QTreeWidgetItem(['first']))        beta.addChild(QtWidgets.QTreeWidgetItem(['second']))        my_tree.expandAll()        alpha.child(0).setSelected(True)        scroll = QtWidgets.QScrollArea()        layout.addWidget(scroll)        scrollLayout = QtWidgets.QVBoxLayout()        scrollW = QtWidgets.QWidget()        scroll.setWidget(scrollW)        scrollW.setLayout(scrollLayout)        scrollLayout.setAlignment(QtCore.Qt.AlignTop)        scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)        scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)        scroll.setWidgetResizable(True)        self.show()app = QtWidgets.QApplication([])window = MainWindow()app.exec_()如何使用户可以选择和单击滚动区域中的每个组?到目前为止,我已尝试在循环中添加以下代码:def onFooGroupClick():    print("Group") fooGroup.clicked.connect(onFooGroupClick)和(根据这篇文章):def onFooGroupClick():    print("Group") def f():    return onFooGroupClick()fooGroup.mousePressEvent = f()然而,我所有的努力都没有成功,我似乎无法让它发挥作用。
查看完整描述

1 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

创建一个继承自QGroupBox. 在其中定义clicked信号并覆盖该mousePressEvent方法。


from PyQt5 import QtWidgets, QtGui, QtCore



class GroupBox(QtWidgets.QGroupBox):                       # +++ !!!

    clicked = QtCore.pyqtSignal(str, object)               # +++


    def __init__(self, title):              

        super(GroupBox, self).__init__()

        self.title = title

        self.setTitle(self.title)


    def mousePressEvent(self, event):

        child = self.childAt(event.pos())

        if not child:

            child = self

        self.clicked.emit(self.title, child)               # +++



class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):

        super(MainWindow, self).__init__()


        w = QtWidgets.QWidget()

        layout = QtWidgets.QVBoxLayout()

        w.setLayout(layout)

        self.setCentralWidget(w)


        my_tree = QtWidgets.QTreeWidget()

        layout.addWidget(my_tree)


        alpha = QtWidgets.QTreeWidgetItem(my_tree, ['Alpha'])

        beta = QtWidgets.QTreeWidgetItem(my_tree, ['Beta'])


        alpha.addChild(QtWidgets.QTreeWidgetItem(['one']))

        alpha.addChild(QtWidgets.QTreeWidgetItem(['two']))


        beta.addChild(QtWidgets.QTreeWidgetItem(['first']))

        beta.addChild(QtWidgets.QTreeWidgetItem(['second']))


        my_tree.expandAll()

        alpha.child(0).setSelected(True)


        scroll = QtWidgets.QScrollArea()

        layout.addWidget(scroll)

        scrollLayout = QtWidgets.QVBoxLayout()


        scrollW = QtWidgets.QWidget()

        scroll.setWidget(scrollW)


        scrollW.setLayout(scrollLayout)

        scrollLayout.setAlignment(QtCore.Qt.AlignTop)


        scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)

        scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

        scroll.setWidgetResizable(True)


        for _ in range(5):

            fooGroup = GroupBox(f'GroupBox_{_}')                           # - QtWidgets.QGroupBox()            

            fooGroup.setObjectName(f'fooGroup {_}')

            fooGroup.clicked.connect(self.onFooGroupClick)                 # +++

            fooLayout = QtWidgets.QVBoxLayout()

            fooGroup.setLayout(fooLayout)

            fooItem1 = QtWidgets.QLabel("fooItem1", objectName="fooItem1")

            fooItem1.setStyleSheet('background: #44ffff')

            fooItem2 = QtWidgets.QLabel("fooItem2", objectName="fooItem2")

            fooItem2.setStyleSheet('background: #ffff56;')

            fooItem3 = QtWidgets.QLabel("fooItem3", objectName="fooItem3")

            fooItem3.setStyleSheet('background: #ff42ff;')

            fooLayout.addWidget(fooItem1)

            fooLayout.addWidget(fooItem2)

            fooLayout.addWidget(fooItem3)

            scrollLayout.addWidget(fooGroup)


    def onFooGroupClick(self, title, obj):                                  # +++

        print(f"Group: {title}; objectName=`{obj.objectName()}`") 



if __name__ == '__main__':

    app = QtWidgets.QApplication([])

    window = MainWindow()

    window.show()

    app.exec_()

//img1.sycdn.imooc.com//6360b69f00014c8b08610309.jpg

查看完整回答
反对 回复 2022-11-01
  • 1 回答
  • 0 关注
  • 194 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信