在 pushButton 中添加小部件?
Add widgets inside pushButton?
嗨,我对 PySide2 和在 Maya 中设置一些 UI 还是比较陌生。
是否有可能有一个 QPushButton 具有其他小部件 children ?
例如,我想制作一对并排的 QPushButtona,当单击其中一个时,它们会相互打开和关闭,每个里面都应该有一个 spinBox。如果 parent 按钮为“关闭”
,则旋转框被禁用
我打算做一个简单的按钮 class,它可以关闭它的对应按钮,所以这一点很简单,但我也看不到将 spinBox 放入其中的方法,也许我错过了吗?我可以向我的按钮添加布局然后在其中添加小部件吗?
感谢您的帮助
本
您实际上可以使用 QGroupBox
,因为您已经可以将其设置为可检查并在其中添加项目。当组框的复选框被勾选时,它将禁用它的任何内容。所以大部分框架已经存在。唯一的问题是而不是复选框,您希望它像 QRadioButton
一样工作,以便另一个禁用。我们可以用一些简单的逻辑来处理:
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
class Win(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
self.resize(300, 0)
# 1st groupbox and spinbox.
self.spinbox1 = QtWidgets.QSpinBox()
self.group_layout1 = QtWidgets.QVBoxLayout()
self.group_layout1.addWidget(self.spinbox1)
self.groupbox1 = QtWidgets.QGroupBox("1")
self.groupbox1.setCheckable(True)
self.groupbox1.setLayout(self.group_layout1)
self.groupbox1.toggled.connect(self.on_groupbox_toggled)
# 2nd groupbox and spinbox.
self.spinbox2 = QtWidgets.QSpinBox()
self.group_layout2 = QtWidgets.QVBoxLayout()
self.group_layout2.addWidget(self.spinbox2)
self.groupbox2 = QtWidgets.QGroupBox("2")
self.groupbox2.setCheckable(True)
self.groupbox2.setChecked(False)
self.groupbox2.setLayout(self.group_layout2)
self.groupbox2.toggled.connect(self.on_groupbox_toggled)
self.main_layout = QtWidgets.QHBoxLayout()
self.main_layout.addWidget(self.groupbox1)
self.main_layout.addWidget(self.groupbox2)
self.setLayout(self.main_layout)
def on_groupbox_toggled(self, enabled):
# Force user to not be able to uncheck it.
if not enabled:
self.sender().setChecked(True)
# Block signals so this function doesn't get triggered by other groupbox.
self.groupbox1.blockSignals(True)
self.groupbox2.blockSignals(True)
# Check off other groupbox.
if self.sender() == self.groupbox1:
self.groupbox2.setChecked(False)
else:
self.groupbox1.setChecked(False)
# Restore signals.
self.groupbox1.blockSignals(False)
self.groupbox2.blockSignals(False)
win = Win()
win.show()
想知道是否可行,看起来可行,您可以使用一个按钮并在其中嵌套另一个小部件。我原以为您必须克服更多困难才能使其正常工作,但这还算不错。
您可以使按钮可检查,然后使用 QButtonGroup
使按钮的行为类似于 QRadioButton
。尽管与 QGroupBox
不同,您需要自己处理旋转框的启用状态。所以这是一个工作示例:
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
class ButtonContainer(QtWidgets.QPushButton):
def __init__(self, parent=None):
super(ButtonContainer, self).__init__(parent)
self.setMinimumHeight(50) # Set minimum otherwise it will collapse the container
self.setCheckable(True)
self.setStyleSheet("""
QPushButton {
background-color: rgb(50, 50, 50);
border: none;
}
QPushButton:checked {
background-color: green;
}
""")
class Win(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
self.resize(300, 0)
# 1st groupbox and spinbox.
self.container_layout1 = QtWidgets.QVBoxLayout()
self.container1 = ButtonContainer()
self.container1.setChecked(True)
self.container1.setLayout(self.container_layout1)
self.container1.clicked.connect(self.on_container_clicked)
self.spinbox1 = QtWidgets.QSpinBox(parent=self.container1) # Set the container as its parent so that we can use `findChildren` in the event later.
self.container_layout1.addWidget(self.spinbox1)
# 2nd groupbox and spinbox.
self.container_layout2 = QtWidgets.QVBoxLayout()
self.container2 = ButtonContainer()
self.container2.setLayout(self.container_layout2)
self.container2.clicked.connect(self.on_container_clicked)
self.spinbox2 = QtWidgets.QSpinBox(parent=self.container2) # Set the container as its parent so that we can use `findChildren` in the event later.
self.container_layout2.addWidget(self.spinbox2)
# Group buttons together so they behave as radio buttons.
self.button_group = QtWidgets.QButtonGroup()
self.button_group.addButton(self.container1)
self.button_group.addButton(self.container2)
self.main_layout = QtWidgets.QHBoxLayout()
self.main_layout.addWidget(self.container1)
self.main_layout.addWidget(self.container2)
self.setLayout(self.main_layout)
# Trigger event to set initial enabled states.
self.on_container_clicked()
def on_container_clicked(self):
for container in [self.container1, self.container2]: # Loop through all of our custom containers.
for child in container.findChildren(QtWidgets.QWidget): # Get all of the container's children.
child.setEnabled(container.isChecked()) # Set its enabled state based if the container is checked.
win = Win()
win.show()
从技术上讲,您实际上并不需要子类化 QPushButton
,尽管这样可以更轻松地回收代码。
嗨,我对 PySide2 和在 Maya 中设置一些 UI 还是比较陌生。 是否有可能有一个 QPushButton 具有其他小部件 children ?
例如,我想制作一对并排的 QPushButtona,当单击其中一个时,它们会相互打开和关闭,每个里面都应该有一个 spinBox。如果 parent 按钮为“关闭”
,则旋转框被禁用我打算做一个简单的按钮 class,它可以关闭它的对应按钮,所以这一点很简单,但我也看不到将 spinBox 放入其中的方法,也许我错过了吗?我可以向我的按钮添加布局然后在其中添加小部件吗?
感谢您的帮助
本
您实际上可以使用 QGroupBox
,因为您已经可以将其设置为可检查并在其中添加项目。当组框的复选框被勾选时,它将禁用它的任何内容。所以大部分框架已经存在。唯一的问题是而不是复选框,您希望它像 QRadioButton
一样工作,以便另一个禁用。我们可以用一些简单的逻辑来处理:
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
class Win(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
self.resize(300, 0)
# 1st groupbox and spinbox.
self.spinbox1 = QtWidgets.QSpinBox()
self.group_layout1 = QtWidgets.QVBoxLayout()
self.group_layout1.addWidget(self.spinbox1)
self.groupbox1 = QtWidgets.QGroupBox("1")
self.groupbox1.setCheckable(True)
self.groupbox1.setLayout(self.group_layout1)
self.groupbox1.toggled.connect(self.on_groupbox_toggled)
# 2nd groupbox and spinbox.
self.spinbox2 = QtWidgets.QSpinBox()
self.group_layout2 = QtWidgets.QVBoxLayout()
self.group_layout2.addWidget(self.spinbox2)
self.groupbox2 = QtWidgets.QGroupBox("2")
self.groupbox2.setCheckable(True)
self.groupbox2.setChecked(False)
self.groupbox2.setLayout(self.group_layout2)
self.groupbox2.toggled.connect(self.on_groupbox_toggled)
self.main_layout = QtWidgets.QHBoxLayout()
self.main_layout.addWidget(self.groupbox1)
self.main_layout.addWidget(self.groupbox2)
self.setLayout(self.main_layout)
def on_groupbox_toggled(self, enabled):
# Force user to not be able to uncheck it.
if not enabled:
self.sender().setChecked(True)
# Block signals so this function doesn't get triggered by other groupbox.
self.groupbox1.blockSignals(True)
self.groupbox2.blockSignals(True)
# Check off other groupbox.
if self.sender() == self.groupbox1:
self.groupbox2.setChecked(False)
else:
self.groupbox1.setChecked(False)
# Restore signals.
self.groupbox1.blockSignals(False)
self.groupbox2.blockSignals(False)
win = Win()
win.show()
想知道是否可行,看起来可行,您可以使用一个按钮并在其中嵌套另一个小部件。我原以为您必须克服更多困难才能使其正常工作,但这还算不错。
您可以使按钮可检查,然后使用 QButtonGroup
使按钮的行为类似于 QRadioButton
。尽管与 QGroupBox
不同,您需要自己处理旋转框的启用状态。所以这是一个工作示例:
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
class ButtonContainer(QtWidgets.QPushButton):
def __init__(self, parent=None):
super(ButtonContainer, self).__init__(parent)
self.setMinimumHeight(50) # Set minimum otherwise it will collapse the container
self.setCheckable(True)
self.setStyleSheet("""
QPushButton {
background-color: rgb(50, 50, 50);
border: none;
}
QPushButton:checked {
background-color: green;
}
""")
class Win(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
self.resize(300, 0)
# 1st groupbox and spinbox.
self.container_layout1 = QtWidgets.QVBoxLayout()
self.container1 = ButtonContainer()
self.container1.setChecked(True)
self.container1.setLayout(self.container_layout1)
self.container1.clicked.connect(self.on_container_clicked)
self.spinbox1 = QtWidgets.QSpinBox(parent=self.container1) # Set the container as its parent so that we can use `findChildren` in the event later.
self.container_layout1.addWidget(self.spinbox1)
# 2nd groupbox and spinbox.
self.container_layout2 = QtWidgets.QVBoxLayout()
self.container2 = ButtonContainer()
self.container2.setLayout(self.container_layout2)
self.container2.clicked.connect(self.on_container_clicked)
self.spinbox2 = QtWidgets.QSpinBox(parent=self.container2) # Set the container as its parent so that we can use `findChildren` in the event later.
self.container_layout2.addWidget(self.spinbox2)
# Group buttons together so they behave as radio buttons.
self.button_group = QtWidgets.QButtonGroup()
self.button_group.addButton(self.container1)
self.button_group.addButton(self.container2)
self.main_layout = QtWidgets.QHBoxLayout()
self.main_layout.addWidget(self.container1)
self.main_layout.addWidget(self.container2)
self.setLayout(self.main_layout)
# Trigger event to set initial enabled states.
self.on_container_clicked()
def on_container_clicked(self):
for container in [self.container1, self.container2]: # Loop through all of our custom containers.
for child in container.findChildren(QtWidgets.QWidget): # Get all of the container's children.
child.setEnabled(container.isChecked()) # Set its enabled state based if the container is checked.
win = Win()
win.show()
从技术上讲,您实际上并不需要子类化 QPushButton
,尽管这样可以更轻松地回收代码。