2

在我的程序中有一个QGroupBox显示,其中有许多QPushButton'。在程序执行期间,用户可以点击外部的按钮,其中的QGroupBox所有按钮都将被移除或隐藏。问题是我似乎找不到直接对按钮执行此操作或通过清除QGroupBox.

我已经尝试deleteLater过按钮,但是没有用。然后我尝试清除layoutQGroupBox但这也不起作用。这是我刚刚写的一些有同样问题的代码:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import random

class UI_Dialog(object):
    def addButtons(self,looping):

        # Code to remove the previous QPushButton's goes here.

        placement = -100
        for i in range(looping):
            currentName = 'btn' + str(i)
            placement = placement + 110
            self.btnB = QtWidgets.QPushButton(self.groupBox)         
            self.btnB.setGeometry(QtCore.QRect(10+placement, 30+placement, 100, 100))
            self.btnB.show()
            self.btnB.setObjectName(currentName)
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(1300, 800)
        self.btnA = QtWidgets.QPushButton(Dialog)
        self.btnA.setGeometry(QtCore.QRect(10, 80, 101, 131))
        self.btnA.setObjectName("btn1")
        self.btnA.clicked.connect(self.pushed)
        self.formLayout = QtWidgets.QFormLayout()
        self.groupBox = QtWidgets.QGroupBox("Results")
        self.groupBox.setLayout(self.formLayout)
        self.resultScrollArea = QtWidgets.QScrollArea(Dialog)
        self.resultScrollArea.setWidget(self.groupBox)
        self.resultScrollArea.setGeometry(QtCore.QRect(20, 220, 1011, 531))
        self.resultScrollArea.setWidgetResizable(True)
        self.resultScrollArea.setObjectName("resultScrollArea")     
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def pushed(self):
        unkownLength = random.randint(1,20)
        self.addButtons(unkownLength)
    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Example Program"))
        self.btnA.setText(_translate("Dialog", "Push Button"))

app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = UI_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())

这里有些东西可能没有意义,比如窗口大小、多个函数调用或者它是一个对话框窗口而不是 QMainWindow。但是,在实际程序的上下文中,它们确实有意义,所以忽略它,我知道它效率低下。此外,该unkownLength变量的全部意义在于模拟在实际程序中,生成的按钮数量将由用户输入确定。按钮也不能在开始时出现,这就是为什么它们是通过单击按钮来创建的。当再次单击该按钮时,它应该删除或隐藏它之前创建的所有按钮。有任何想法吗?

4

1 回答 1

1

利用按钮是子级的优势,QGroupBox我们可以获得使用findChildren()to use的按钮deleteLater()

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import random


class UI_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(1300, 800)
        self.btnA = QtWidgets.QPushButton(Dialog)
        self.btnA.setGeometry(QtCore.QRect(10, 80, 101, 131))
        self.btnA.setObjectName("btn1")

        self.formLayout = QtWidgets.QFormLayout()
        self.groupBox = QtWidgets.QGroupBox("Results")
        self.groupBox.setLayout(self.formLayout)
        self.resultScrollArea = QtWidgets.QScrollArea(Dialog)
        self.resultScrollArea.setWidget(self.groupBox)
        self.resultScrollArea.setGeometry(QtCore.QRect(20, 220, 1011, 531))
        self.resultScrollArea.setWidgetResizable(True)
        self.resultScrollArea.setObjectName("resultScrollArea")
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Example Program"))
        self.btnA.setText(_translate("Dialog", "Push Button"))


class Dialog(QtWidgets.QDialog, UI_Dialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setupUi(self)
        self.btnA.clicked.connect(self.pushed)

    @QtCore.pyqtSlot()
    def pushed(self):
        unkownLength = random.randint(1, 20)
        self.addButtons(unkownLength)

    def addButtons(self, looping):
        for button in self.groupBox.findChildren(QtWidgets.QPushButton):
            button.deleteLater()
        placement = -100
        pos = QtCore.QPoint(20, 40)
        for i in range(looping):
            currentName = "btn" + str(i)
            self.btnB = QtWidgets.QPushButton(
                self.groupBox, objectName=currentName
            )
            self.btnB.setGeometry(QtCore.QRect(pos, QtCore.QSize(100, 100)))
            pos += QtCore.QPoint(110, 110)
            self.btnB.show()


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Dialog()
    w.show()
    sys.exit(app.exec_())
于 2019-04-08T14:51:05.697 回答