猿问

使 QGroupBox 可选择和可点击

我有以下玩具界面:


from PyQt5 import QtWidgets, QtGui, QtCore


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)

        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()

然而,我所有的努力都没有成功,我似乎无法让它发挥作用。


慕尼黑的夜晚无繁华
浏览 196回答 1
1回答

慕标5832272

创建一个继承自QGroupBox. 在其中定义clicked信号并覆盖该mousePressEvent方法。from PyQt5 import QtWidgets, QtGui, QtCoreclass 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_()
随时随地看视频慕课网APP

相关分类

Python
我要回答