mercredi 26 juillet 2017

Programmatically calling toggle on a checkbox breaks gui in pyqt5

I am creating a settings widget where a user clicks check boxes to select their preferred settings. I want to have a 'Discard Changes' button that when pressed reverts the checkbox selections to the last save. To do this i had saved the states of the check boxes from the last save in a dictionary, when discard changes is called, it will cycle through the dictionary, and if the checkbox's value is True, i will toggle, if False i will untoggle. When doing this, the gui breaks and checkboxes disappear. I've experimented and found out that if i call x.toggle() once, it will work, but if toggle is called again on a different checkbox it will break. Any help will be appreciated!

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 = {}

    self.metaCommandFileName.toggled.connect(lambda: self.checkManager(('CommandFileName','1234_ExampleCommandFile_32psi',self.metaCommandFileName)))
    self.metaConstructionCode.toggled.connect(lambda: self.checkManager(('ConstructionCode','XYZ123',self.metaConstructionCode)))
    self.metaDescription.toggled.connect(lambda: self.checkManager(('Description','ExtraGrip LS2',self.metaDescription)))
    self.metaDiameter.toggled.connect(lambda: self.checkManager(('Diameter','19.0',self.metaDiameter)))
    self.metaManufacturerDesc.toggled.connect(lambda: self.checkManager(('ManufacturerDesc','Goodhama',self.metaManufacturerDesc)))
    self.metaRimDescription.toggled.connect(lambda: self.checkManager(('RimDescription','LAB 19x8',self.metaRimDescription)))
    self.metaRimMaterialDesc.toggled.connect(lambda: self.checkManager(('RimMaterialDesc','Lab Steel',self.metaRimMaterialDesc)))
    self.metaSerialNumber.toggled.connect(lambda: self.checkManager(('SerialNumber','EST31277321',self.metaSerialNumber)))
    self.metaSize.toggled.connect(lambda: self.checkManager(('Size','235/40R19 (99Y)',self.metaSize)))
    self.metaTestTypeDesc.toggled.connect(lambda: self.checkManager(('TestTypeDesc','Braking Driving Cornering',self.metaTestTypeDesc)))
    self.metaTestTypeID.toggled.connect(lambda: self.checkManager(('TestTypeID','4',self.metaTestTypeID)))
    self.metaTitle.toggled.connect(lambda: self.checkManager(('Title','Title',self.metaTitle)))
    self.metaWidth.toggled.connect(lambda: self.checkManager(('Width','8.0',self.metaWidth)))
    self.saveChangesB.clicked.connect(self.saveChanges)
    self.discardChangesB.clicked.connect(self.discardChanges)
    self.__guiSetup__()

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 updatePlot(self):
    try:
        self.metaLayout.removeWidget(self._canvas)
    except:
        pass

    plt.figure()
    p1, = plt.plot(1,color='r')
    p2, = plt.plot(2,color='b')
    blank = Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)

    header = ""
    for property in self.descList:
        header += (' '+property+' ')

    plt.legend([blank, p1, p2], [header, 'Raw Data', 'Trimmed Data'],loc='upper right')
    self._canvas = FigureCanvas(plt.gcf())
    self.metaLayout.addWidget(self._canvas)

def checkManager(self, property):
    '''Manages tire description check boxes'''
    if (' ' + property[1] +' ') in self.metaCustomLine.text():
        temp = self.metaCustomLine.text()
        temp = temp.replace(' '+property[1]+' ','')
        self.metaCustomLine.setText(temp)
        self.metaHeader.remove(property[0])
        self.tempSettings[property[2]] = False
    else:
        self.metaCustomLine.setText(self.metaCustomLine.text() + ' '+property[1]+' ')
        self.metaHeader.append(property[0])
        self.tempSettings[property[2]] = True
    self.descList = self.metaCustomLine.text().split()
    self.updatePlot()

def saveChanges(self):
    self.parent.metadata.globalHeader = self.metaHeader
    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




Aucun commentaire:

Enregistrer un commentaire