How to create a list of checkboxes

前端 未结 1 432
情话喂你
情话喂你 2021-01-21 17:09

I\'m trying to read in an xml file and populate a QListWidget with some of its contents. Each entry should have a checkbox.

In Qt Designer I created the lis

1条回答
  •  野的像风
    2021-01-21 17:27

    You're almost there: it's just that you're trying to make the list-widget checkable, rather than each list-widget-item.

    Try this instead:

    from PyQt5 import QtWidgets, QtCore
    
    self.listWidgetTestCases = QtWidgets.QListWidget()
    
    for testcases in root.iter('testcase'):
        testcase_name = str(testcases.attrib)
    
        item = QtWidgets.QListWidgetItem(testcase_name)
        item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
        item.setCheckState(QtCore.Qt.Unchecked)
        self.listWidgetTestCases.addItem(item)
    

    (NB: for PyQt4, use QtGui instead of QtWidgets)

    0 讨论(0)
提交回复
热议问题