以编程方式调用复选框上的切换会破坏pyqt5中的gui

时间:2017-07-26 12:13:46

标签: python checkbox pyqt pyqt5

我正在创建一个设置小部件,用户单击复选框以选择其首选设置。我想要一个'放弃更改'按钮,按下该按钮会将复选框选择恢复为上次保存。

为了做到这一点,我保存了字典中最后一次保存的复选框的状态,当调用discard更改时,它将循环遍历字典,如果复选框的值为True,我将切换,如果为False我会解开。这样做时,gui断开并且复选框消失。

我已经进行了实验并发现,如果我调用x.setChecked(True)一次,它会起作用,但是如果在另一个复选框上再次调用切换它将会中断。任何帮助将不胜感激!

class GlobalSettings(QtWidgets.QWidget, Ui_Form):

    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.parent = parent
        self.setupUi(self)
        self.descList = [] #list that holds tire description elements in order
        self.metaHeader = []
        self.savedSettings = {}
        self.tempSettings = {}



    def __guiSetup__(self):
        self.metaCustomLine.setReadOnly(True)
        self.metaConstructionCode.toggle()

        self.tempSettings[self.metaCommandFileName] = False
        self.tempSettings[self.metaConstructionCode] = True
        self.tempSettings[self.metaDescription] = False
        self.tempSettings[self.metaDiameter] = False
        self.tempSettings[self.metaManufacturerDesc] = False
        self.tempSettings[self.metaRimDescription] = False
        self.tempSettings[self.metaRimMaterialDesc] = False
        self.tempSettings[self.metaSerialNumber] = False
        self.tempSettings[self.metaSize] = False
        self.tempSettings[self.metaTestTypeDesc] = False
        self.tempSettings[self.metaTestTypeID] = False
        self.tempSettings[self.metaTitle] = False
        self.tempSettings[self.metaWidth] = False

        for x in self.tempSettings:
            self.savedSettings[x] = self.tempSettings[x]


    def discardChanges(self):
        for x in self.savedSettings:
            if self.savedSettings[x]:
                x.setChecked(True)
            else:
                x.setChecked(False)


    def refresh(self):
        return 1

1 个答案:

答案 0 :(得分:0)

在循环中处理信号时,似乎需要调用QtWidgets.QApplication.processEvents()。

def discardChanges(self):
    for x in self.savedSettings:
        if self.savedSettings[x]:
            x.setChecked(True)
            QtWidgets.QApplication.processEvents()
        else:
            x.setChecked(False)
            QtWidgets.QApplication.processEvents()