第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使 QGroupBox 可選擇和可點(diǎn)擊

使 QGroupBox 可選擇和可點(diǎn)擊

慕尼黑的夜晚無繁華 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_()如何使用戶可以選擇和單擊滾動(dòng)區(qū)域中的每個(gè)組?到目前為止,我已嘗試在循環(huán)中添加以下代碼:def onFooGroupClick():    print("Group") fooGroup.clicked.connect(onFooGroupClick)和(根據(jù)這篇文章):def onFooGroupClick():    print("Group") def f():    return onFooGroupClick()fooGroup.mousePressEvent = f()然而,我所有的努力都沒有成功,我似乎無法讓它發(fā)揮作用。
查看完整描述

1 回答

?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊

創(chuàng)建一個(gè)繼承自QGroupBox. 在其中定義clicked信號(hào)并覆蓋該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_()

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

查看完整回答
反對(duì) 回復(fù) 2022-11-01
  • 1 回答
  • 0 關(guān)注
  • 241 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)