是否可以为所有 QDoubleSpinBox 仅设置 100% 的价值共享限制?

Is it possible to set only 100 Percent value share limit to all QDoubleSpinBoxes?

我想与所有 QDoubleSpinBox 共享 100% 的值限制。如果可以怎么办?

    self.SpinBox_one = QtWidgets.QDoubleSpinBox()
    self.SpinBox_two = QtWidgets.QDoubleSpinBox()
    self.SpinBox_three = QtWidgets.QDoubleSpinBox()
    self.SpinBox_four = QtWidgets.QDoubleSpinBox()
    self.SpinBox_five = QtWidgets.QDoubleSpinBox()

例如,我有 5 个 QDoubleSpinBoxes,如上。 如果我在 self.SpinBox_one 中插入 30,剩下的是 70,那么剩下的 4 个 QDoubleSpinBox 应该只有 70 个有用。再一次,如果我在 self.SpinBox_two 中插入 50,Remaining 应该是 20,它应该对剩余的 QDoubleSpinBoxes 有用。再次,如果我在 self.SpinBox_three 中插入 20,则剩余为 0。现在剩余的 QDoubleSpinBoxes 应该为 0。 下面是我的示例代码:

from PyQt5 import QtWidgets
from PyQt5 import QtCore

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.SpinBox_one = QtWidgets.QDoubleSpinBox()
        self.SpinBox_two = QtWidgets.QDoubleSpinBox()
        self.SpinBox_three = QtWidgets.QDoubleSpinBox()
        self.SpinBox_four = QtWidgets.QDoubleSpinBox()
        self.SpinBox_five = QtWidgets.QDoubleSpinBox()

        form_widget = QtWidgets.QWidget()
        form_layout = QtWidgets.QFormLayout(form_widget)
        form_layout.addRow("1", self.SpinBox_one)
        form_layout.addRow("2", self.SpinBox_two)
        form_layout.addRow("3", self.SpinBox_three)
        form_layout.addRow("4", self.SpinBox_four)
        form_layout.addRow("5", self.SpinBox_five)

        form_widget.setFixedSize(form_widget.sizeHint())
        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        hlay1 = QtWidgets.QHBoxLayout()
        hlay1.addWidget(form_widget)
        hlay1.addStretch()

        vboxlayout = QtWidgets.QVBoxLayout(central_widget)
        vboxlayout.addLayout(hlay1)
        self.resize(50, 50)

        ##
        self.SpinBox_one.setMaximum(100.00)
        #self.SpinBox_two.setMaximum(100.00)
        #self.SpinBox_three.setMaximum(100.00)
        #self.SpinBox_four.setMaximum(100.00)
        #self.SpinBox_five.setMaximum(100.00)

        self.SpinBox_one.valueChanged.connect(self.SpinBox_one_changed)
        self.SpinBox_two.valueChanged.connect(self.SpinBox_two_changed)
        self.SpinBox_three.valueChanged.connect(self.SpinBox_three_changed)
        self.SpinBox_four.valueChanged.connect(self.SpinBox_four_changed)
    def SpinBox_one_changed(self):
         self.SpinBox_two.setMaximum(100 - self.SpinBox_one.value())

    def SpinBox_two_changed(self):
         self.SpinBox_three.setMaximum(100-self.SpinBox_one.value() - self.SpinBox_two.value())
         
    def SpinBox_three_changed(self):
         self.SpinBox_four.setMaximum(100-self.SpinBox_one.value() - self.SpinBox_two.value() - self.SpinBox_three.value())

    def SpinBox_four_changed(self):
         self.SpinBox_five.setMaximum(100-self.SpinBox_one.value() - self.SpinBox_two.value() - self.SpinBox_three.value() - self.SpinBox_four.value())

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

第四个正在运行,但第五个未按预期运行。

在旋转框列表上操作比为每个旋转框保留一个单独的名称更简单。每个 QDoubleSpinBox 的 valueChanged 信号都连接到限制剩余框的最大值的函数 limit()。首先统计刚刚更改的旋转框的总值。然后对于它后面的旋转框,将它们的最大值设置为 100 中的剩余数量并增加 运行 总数。

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        form_widget = QtWidgets.QWidget()
        form_layout = QtWidgets.QFormLayout(form_widget)

        self.boxes = []
        for i in range(5):
            box = QtWidgets.QDoubleSpinBox(maximum=100, valueChanged=self.limit)
            form_layout.addRow(str(i + 1), box)
            self.boxes.append(box)

        form_widget.setFixedSize(form_widget.sizeHint())
        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        hlay1 = QtWidgets.QHBoxLayout()
        hlay1.addWidget(form_widget)
        hlay1.addStretch()

        vboxlayout = QtWidgets.QVBoxLayout(central_widget)
        vboxlayout.addLayout(hlay1)
        self.resize(50, 50)

    def limit(self, value):
        i = self.boxes.index(self.sender())
        total = value + sum(box.value() for box in self.boxes[:i])
        for box in self.boxes[i + 1:]:
            box.blockSignals(True) # Avoid extra calls to limit()
            box.setMaximum(100 - total)
            box.blockSignals(False)
            total += box.value()

编辑,保留原来的__init__代码:

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.SpinBox_one = QtWidgets.QDoubleSpinBox()
        self.SpinBox_two = QtWidgets.QDoubleSpinBox()
        self.SpinBox_three = QtWidgets.QDoubleSpinBox()
        self.SpinBox_four = QtWidgets.QDoubleSpinBox()
        self.SpinBox_five = QtWidgets.QDoubleSpinBox()

        form_widget = QtWidgets.QWidget()
        form_layout = QtWidgets.QFormLayout(form_widget)
        form_layout.addRow("1", self.SpinBox_one)
        form_layout.addRow("2", self.SpinBox_two)
        form_layout.addRow("3", self.SpinBox_three)
        form_layout.addRow("4", self.SpinBox_four)
        form_layout.addRow("5", self.SpinBox_five)
        
        form_widget.setFixedSize(form_widget.sizeHint())
        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        hlay1 = QtWidgets.QHBoxLayout()
        hlay1.addWidget(form_widget)
        hlay1.addStretch()

        vboxlayout = QtWidgets.QVBoxLayout(central_widget)
        vboxlayout.addLayout(hlay1)
        self.resize(50, 50)

        ##
        self.SpinBox_one.setMaximum(100.00)
        #self.SpinBox_two.setMaximum(100.00)
        #self.SpinBox_three.setMaximum(100.00)
        #self.SpinBox_four.setMaximum(100.00)
        #self.SpinBox_five.setMaximum(100.00)

        self.SpinBox_one.valueChanged.connect(self.limit)
        self.SpinBox_two.valueChanged.connect(self.limit)
        self.SpinBox_three.valueChanged.connect(self.limit)
        self.SpinBox_four.valueChanged.connect(self.limit)
        
        self.boxes = [self.SpinBox_one, self.SpinBox_two, self.SpinBox_three, self.SpinBox_four, self.SpinBox_five]