mardi 22 janvier 2019

How can I display boolean item as checkbox in QTableWidget?

I am fairly new at Pyside, I would like to display a boolean datatype as a checkbox within a tablewidget

I have tried reconstructing the Data with a QTableWidgetItem but it did not work (see commented out section)

rows = [('Test1123456789', False), ('Test2123456789', False), ('Test3123456789', True), ('Test4123456789', True)]

    #rows2 = []

    # self.checkbox2 = QtWidgets.QTableWidgetItem()
    # self.checkbox2.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)

    # #for i in rows:
    #   #print(i)
    #   #if i[1] == False:
    #       #self.newi = self.checkbox2
    #       self.newi = self.newi.setCheckState(QtCore.Qt.Unchecked)
    #       rows2.append((i[0],self.newi))
    #   else:
    #       self.newi = self.checkbox2
    #       self.newi = self.newi.setCheckState(QtCore.Qt.Checked)
    #       rows2.append((i[0],self.newi))

    for row_number, row_data in enumerate(rows):
        self.classestable.insertRow(row_number)
        for colum_number, data in enumerate(row_data):
            colum_number = colum_number +1
            self.checkbox = QtWidgets.QTableWidgetItem()
            self.checkbox.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
            self.checkbox.setCheckState(QtCore.Qt.Unchecked)
            self.classestable.setItem(row_number,0,self.checkbox)
            self.classestable.setItem(row_number,colum_number,QtWidgets.QTableWidgetItem(str(data)))
    self.classestable.resizeColumnsToContents()

I would like to have it return in the table

CheckBox(Unchecked), Test1123456789, CheckBox(Unchecked)
CheckBox(Unchecked), Test2123456789, CheckBox(Unchecked)
CheckBox(Unchecked), Test3123456789, CheckBox(Checked)
CheckBox(Unchecked), Test4123456789, CheckBox(Checked)




Aucun commentaire:

Enregistrer un commentaire