Make QGroupBox selectable and clickable

懵懂的女人 提交于 2021-02-11 17:52:25

问题


I have the following toy interface:

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)

        for _ in range(5):
            fooGroup = QtWidgets.QGroupBox()
            fooLayout = QtWidgets.QVBoxLayout()
            fooGroup.setLayout(fooLayout)
            fooItem1 = QtWidgets.QLabel("fooItem1")
            fooItem2 = QtWidgets.QLabel("fooItem2")
            fooItem3 = QtWidgets.QLabel("fooItem3")
            fooLayout.addWidget(fooItem1)
            fooLayout.addWidget(fooItem2)
            fooLayout.addWidget(fooItem3)
            scrollLayout.addWidget(fooGroup)

        self.show()

app = QtWidgets.QApplication([])
window = MainWindow()
app.exec_()

How can I make each group in the scroll area selectable and clickable by the user?

I have so far tried to add the following code in the loop:

def onFooGroupClick():
    print("Group") 

fooGroup.clicked.connect(onFooGroupClick)

and (as per this post):

def onFooGroupClick():
    print("Group") 

def f():
    return onFooGroupClick()

fooGroup.mousePressEvent = f()

However, all my efforts have been unsuccessful and I cannot seem to be able to make it work.


回答1:


Create a class that inherits from QGroupBox. Define the clicked signal in it and override the mousePressEvent method.

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



来源:https://stackoverflow.com/questions/61520341/make-qgroupbox-selectable-and-clickable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!