要在按钮内添加小部件吗?

问题描述 投票:-2回答:2

嗨,我对PySide2还是比较陌生,并在maya中设置了一些用户界面。是否可以有一个QPushButton,它具有其他小部件作为子项?

例如,我想制作一对QPushButtona并排放置,当单击其中一个时,它们会彼此打开和关闭,每一个都应该在其中包含一个spinBox。如果spinBox的父按钮为“ off”,则其将被禁用。

我本来要制作一个简单的按钮类,可以将其对应的按钮关闭,所以这很简单,但是我看不到将spinBox放入其中的方法,也许我错过了吗?我可以在按钮上添加布局然后在其中添加小部件吗?

感谢您的帮助

Ben

python pyside maya pyside2
2个回答
1
投票

您实际上可以使用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()

Example


0
投票

想知道是否有可能,而且看起来像是,您可以使用按钮并在其中嵌套另一个小部件。您可以使按钮处于选中状态,然后使用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

© www.soinside.com 2019 - 2024. All rights reserved.