根据来自 QComboBox 的用户输入添加和删除动态生成的 QLineEdit 小部件

Posted

技术标签:

【中文标题】根据来自 QComboBox 的用户输入添加和删除动态生成的 QLineEdit 小部件【英文标题】:Adding and removing dynamically generated QLineEdit widgets based off of user input from a QComboBox 【发布时间】:2019-02-17 22:43:50 【问题描述】:

我正在构建一个应用程序来“滚动”同一面的多个骰子,所以如果我需要滚动 5 个 8 面骰子,我在标签 D8 下的 QComboBox 中选择 5,然后会弹出 5 个 QLineEdit 小部件,其中包含值显示的 5 个“掷骰子”。

目前,在选择 QComboBox 中的数字后,我能够显示正确数量的 QLineEdit 小部件。当我尝试完全移除骰子显示或将 QLineEdit 小部件的数量设置为小于第一个值集时,问题就出现了。有时它会起作用,但当我尝试将显示设置为 0 时它永远不会,其他时候我会抛出 KeyError 或 RuntimeError。

from PySide2.QtCore import Qt
from PySide2.QtWidgets import QGridLayout, QPushButton, QLineEdit, QLabel, QComboBox, QDialog, QApplication

class RollMultiDiePopup(QDialog):
    def __init__(self, parent=None):
        super(RollMultiDiePopup, self).__init__(parent)
        #self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('Additional Dies to roll')
        self.setContentsMargins(5, 5, 5, 5)
        self.comboDict = "D4": 4, "D6": 6, "D8": 8, "D10": 10, "D12": 12, "D20": 20
        self.comboLblDict = "# of D4s": 0, "# of D6s": 0, "# of D8s": 0, "# of D10s": 0, "# of D12s": 0, "# of D20s": 0
        self.layoutGrid = QGridLayout(self)
        self.layoutGrid.setSpacing(10)
        self.gridRow = 0
        self.gridCol = 0
        self.comboLbl = 
        self.comboBoxes = 
        self.addnlInputs = 
        self.generatecombolabels()
        self.generatecomboboxes()
        self.generaterollbuttons()
        self.setLayout(self.layoutGrid)
        self.adjustSize()

    def generatecombolabels(self):
        self.gridCol = 0
        for key, val in self.comboLblDict.items():
            self.gridCol = self.gridCol + 1
            self.comboLbl[key] = QLabel(key, self)
            self.layoutGrid.addWidget(self.comboLbl[key], 0, self.gridCol)

    def generatecomboboxes(self):
        self.gridCol = 0
        for key, val in self.comboDict.items():
            self.gridCol = self.gridCol + 1
            self.comboBoxes[key] = QComboBox(self)
            self.comboBoxes[key].addItems([str(x) for x in range(21)])
            #self.comboBoxes[key].activated.connect(self.adddisplays)
            self.layoutGrid.addWidget(self.comboBoxes[key], 1, self.gridCol)

    def generaterollbuttons(self):
        self.gridCol = 0
        for key, val in self.comboDict.items():
            self.gridCol = self.gridCol + 1
            buttons = QPushButton("Roll " + key + "s", self)
            buttons.setToolTip("Roll D" + str(self.comboDict[key]) + " (1 - " + str(self.comboDict[key]) + ")")
            #buttons.clicked.connect(partial(self.rolldie, val))
            buttons.clicked.connect(self.adddisplays)
            self.layoutGrid.addWidget(buttons, 2, self.gridCol)

    def rolldie(self):
        pass

    def adddisplays(self):
        d4s = int(self.comboBoxes["D4"].currentText())
        d6s = int(self.comboBoxes["D6"].currentText())
        d8s = int(self.comboBoxes["D8"].currentText())
        d10s = int(self.comboBoxes["D10"].currentText())
        d12s = int(self.comboBoxes["D12"].currentText())
        d20s = int(self.comboBoxes["D20"].currentText())
        dies = 1: d4s, 2: d6s, 3: d8s, 4: d10s, 5: d12s, 6: d20s
        #if d4s == 0 or d6s == 0 or d8s == 0 or d10s == 0 or d12s == 0 or d20s == 0:
        self.removeaddeddisplays()
        if d4s > 0 or d6s > 0 or d8s > 0 or d10s > 0 or d12s > 0 or d20s > 0:
            for keys, vals in dies.items():
                self.gridRow = 3
                for i in range(vals):
                    self.gridRow += 1
                    self.addnlInputs["addnlInput" + str(i + 1)] = QLineEdit(self)
                    self.addnlInputs["addnlInput" + str(i + 1)].setAlignment(Qt.AlignRight)
                    self.addnlInputs["addnlInput" + str(i + 1)].setText("")
                    self.addnlInputs["addnlInput" + str(i + 1)].setPlaceholderText("Die Roll #" + str(i + 1))
                    self.layoutGrid.addWidget(self.addnlInputs["addnlInput" + str(i + 1)], self.gridRow, keys)

    def removeaddeddisplays(self):
        try:
            for i in range(3, 21):
                self.layoutGrid.removeWidget(self.addnlInputs["addnlInput" + str(i + 1)])
                self.addnlInputs["addnlInput" + str(i + 1)].deleteLater()
                self.addnlInputs["addnlInput" + str(i + 1)] = None
                self.adjustSize()
        except KeyError:
            print("1")
        except RuntimeError:
            print("2")


if __name__ == "__main__":
    app = QApplication()
    w = RollMultiDiePopup()
    w.show()
    app.exec_()

这是最初生成 QComboBoxes 并填充 #s 列表的代码。

self.gridCol = 0
for key, val in self.comboDict.items():
    self.gridCol = self.gridCol + 1
    self.comboBoxes[key] = QComboBox(self)
    self.comboBoxes[key].addItems([str(x) for x in range(21)])
    #self.comboBoxes[key].activated.connect(self.adddisplays)
    self.layoutGrid.addWidget(self.comboBoxes[key], 1, self.gridCol)

这是添加与D#标签/组合框对应的按钮时显示的代码

d4s = int(self.comboBoxes["D4"].currentText())
d6s = int(self.comboBoxes["D6"].currentText())
d8s = int(self.comboBoxes["D8"].currentText())
d10s = int(self.comboBoxes["D10"].currentText())
d12s = int(self.comboBoxes["D12"].currentText())
d20s = int(self.comboBoxes["D20"].currentText())
dies = 1: d4s, 2: d6s, 3: d8s, 4: d10s, 5: d12s, 6: d20s
#if d4s == 0 or d6s == 0 or d8s == 0 or d10s == 0 or d12s == 0 or d20s == 0:
self.removeaddeddisplays()
if d4s > 0 or d6s > 0 or d8s > 0 or d10s > 0 or d12s > 0 or d20s > 0:
    for keys, vals in dies.items():
        self.gridRow = 3
        for i in range(vals):
            self.gridRow += 1
            self.addnlInputs["addnlInput" + str(i + 1)] = QLineEdit(self)
            self.addnlInputs["addnlInput" + str(i + 1)].setAlignment(Qt.AlignRight)
            self.addnlInputs["addnlInput" + str(i + 1)].setText("")
            self.addnlInputs["addnlInput" + str(i + 1)].setPlaceholderText("Die Roll #" + str(i + 1))
            self.layoutGrid.addWidget(self.addnlInputs["addnlInput" + str(i + 1)], self.gridRow, keys)

这里是 removeaddeddisplays 函数

try:
    for i in range(3, 21):
        self.layoutGrid.removeWidget(self.addnlInputs["addnlInput" + str(i + 1)])
        self.addnlInputs["addnlInput" + str(i + 1)].deleteLater()
        self.addnlInputs["addnlInput" + str(i + 1)] = None
        self.adjustSize()
except KeyError:
     print("1")
except RuntimeError:
     print("2")

我尝试了几种不同的方法来删除小部件并得到不同的结果,只是删除了所有其他显示,所有内容,每隔三分之一..等等。这是迄今为止我发现的最一致的方式。

我正在使用 python 3 和 pyside 2,在显示器正常工作后,我会担心如何处理骰子。

【问题讨论】:

第一个问题,D4和D8有什么区别?,数字4是否表示QComboBox应该取1到4的值,8表示它对应的QComboBox应该取1到8的值? D4 是要掷出的 4 面骰子的数量,D8 是要掷出的 8 面骰子的数量。 QComboBox 将告诉 rolldie 函数滚动 X 个 4、6、8、10、12 和 20 面骰子。然后在正在生成的 QLineEdits 中显示滚动结果。我只想生成掷骰子数量所需的 QLineEdits 数量。我可以做得很好,问题是当我删除输入以滚动较少数量的骰子时。 i.imgur.com/nU0sxjw.png - 没有显示的 gui 截图 i.imgur.com/AyimE7G.png - 有显示的 gui 截图 【参考方案1】:

如果你想从布局中删除一个小部件,你只需要使用 deleteLater 但这并不意味着如果引用存储在列表中就会被删除。在我的策略中,我使用 deleteLater 消除了所有 QLineEdits,我重置了存储它的列表并创建了新的 QLineEdits。

from PySide2 import QtCore, QtWidgets

class RollMultiDiePopup(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(RollMultiDiePopup, self).__init__(parent)

        self._data = 
            "D4": 4, 
            "D6": 6, 
            "D8": 8, 
            "D10": 10, 
            "D12": 12, 
            "D20": 20
        

        self._lineedits = [[] for _ in self._data]
        self._comboboxes = []

        grid_layout = QtWidgets.QGridLayout(self)
        self.create_labels()
        self.create_comboboxes()
        self.create_buttons()

    def create_labels(self):
        row = 0
        grid_layout = self.layout()
        for i, k in enumerate(self._data.keys()):
            label = QtWidgets.QLabel("# of s".format(k))
            grid_layout.addWidget(label, row, i)

    def create_comboboxes(self):
        row = 1
        grid_layout = self.layout()
        for i in range(len(self._data)):
            combo = QtWidgets.QComboBox()
            combo.addItems([str(j) for j in range(21)])
            self._comboboxes.append(combo)
            grid_layout.addWidget(combo, row, i)

    def create_buttons(self):
        row = 2
        grid_layout = self.layout()
        for i, (k, v) in enumerate(self._data.items()):
            button = QtWidgets.QPushButton("Roll s".format(k))
            button.setToolTip("Roll (1 - )".format(k, v))
            button.clicked.connect(self.update_lineedits)
            grid_layout.addWidget(button, row, i)

    @QtCore.Slot()
    def update_lineedits(self):
        row = 3
        grid_layout = self.layout()
        for r in self._lineedits:
            for le in r:
                le.deleteLater()
        self._lineedits = [[] for _ in self._data]
        for i, (les,combo) in enumerate(zip(self._lineedits, self._comboboxes)):
            v = int(combo.currentText())
            for j in range(v):
                le = QtWidgets.QLineEdit()
                le.setPlaceholderText("Die Roll #".format(j+1))
                grid_layout.addWidget(le, row+j, i)
                les.append(le)
        QtCore.QTimer.singleShot(0, self.adjust_size)

    @QtCore.Slot()
    def adjust_size(self):
        animation = QtCore.QPropertyAnimation(self, b"size", self)
        animation.setStartValue(self.size())
        animation.setEndValue(self.minimumSizeHint())
        animation.start(QtCore.QAbstractAnimation.DeleteWhenStopped)

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

【讨论】:

这很好用!但是,当您将它们全部设置回 0 时,会发生这种情况:i.imgur.com/tGqDkUs.png 我在 update_lineedits 函数中的任何地方都尝试过 self.adjustSize(),但它看起来仍然如此。 如果我想更改使用您的方法生成的其中一个 qlineedits 的文本,我该怎么做?如果不是全部,我无法弄清楚如何在任何单个 qlineedit 上执行 setText。 使用self._lineedits[index_i][index_j].setText(">>>>>")

以上是关于根据来自 QComboBox 的用户输入添加和删除动态生成的 QLineEdit 小部件的主要内容,如果未能解决你的问题,请参考以下文章

如何从另一个类 PyQT 向 QComboBox 添加项目?

根据内容的变化从 QWizardPage 动态添加/删除 Finish

PyQt5 组件之QComboBox

QListWidget的QComboBox下拉列表添加复选框及消息处理

Pyqt5_QComboBox

如何在 PyQt/PySide 中将项目添加到 QComboBox