在按钮内添加小部件?

2024-09-24 14:25:28 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我对PySide2还是比较新的,在maya中设置了一些UI。 有没有可能有一个QPushButton,它有其他小部件作为子部件?你知道吗

例如,我想制作一对并排的QPushButtona,当其中一个被单击时,它们可以打开或关闭,每一个都应该有一个spinBox。如果spinBox的父按钮为“off”,则该spinBox将被禁用

我本来打算做一个简单的button类,可以关闭它的对应项,所以这一点很简单,但是我看不到一个方法可以把spinBox也放进去,也许我错过了什么?我能给我的按钮添加一个布局然后在里面添加小部件吗?你知道吗

谢谢你的帮助


Tags: 方法ui部件button布局按钮offpyside2
2条回答

实际上,您可以使用QGroupBox,因为您已经可以使其可检查并在其中添加项。当选中groupbox的复选框时,它将禁用其任何内容。所以大部分框架已经存在了。唯一的问题不是复选框,而是希望它像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()

Example

想知道这是否可能,看起来是的,您可以使用一个按钮并在其中嵌套另一个小部件。我原以为你得多跳几圈才能让它正常工作,但也不算太糟。你知道吗

您可以使按钮可检查,然后使用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进行子类化,尽管这样可以更容易地回收代码。你知道吗

Example

相关问题 更多 >