使 QGroupBox 可选择和可点击 [英] Make QGroupBox selectable and clickable
本文介绍了使 QGroupBox 可选择和可点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下玩具界面:
from PyQt5 import QtWidgets, QtGui, QtCore类 MainWindow(QtWidgets.QMainWindow):def __init__(self):super(MainWindow, self).__init__()w = QtWidgets.QWidget()布局 = QtWidgets.QVBoxLayout()w.setLayout(布局)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)滚动 = QtWidgets.QScrollArea()layout.addWidget(滚动)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)对于 _ 范围(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)自我展示()app = QtWidgets.QApplication([])窗口 = 主窗口()app.exec_()
如何让滚动区域中的每个组都可以被用户选择和点击?
到目前为止,我已尝试在循环中添加以下代码:
def onFooGroupClick():打印(组")fooGroup.clicked.connect(onFooGroupClick)
和(根据此
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.
解决方案
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_()
这篇关于使 QGroupBox 可选择和可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文