PDA

View Full Version : Is it possible to populate a list from a file so each entry has a checkbox?



pixxyman
19th March 2015, 15:52
Hi,

I'm using Python and trying to read in an xml file and populate a list QT ListWidget with some of its contents. Each entry should have a checkbox.

In QT I created the list and added an item that has a checkbox by adding the item to the listWidget, then right clicking on it and selecting Edit Items > Properties > Set Flags to UserCheckable. So i can do it manually in QT.

But when I read in the xml file to populate the ListWidget I can't make these items checkable.

import xml.etree.ElementTree as et

xml_file = os.path.join(path, testcase_file)
tree = et.parse(xml_file)
root = tree.getroot()

for testcases in root.iter('testcase'):
testcase_name = str(testcases.attrib)
item = self.listWidgetTestCases
item.addItem(QtGui.QApplication.translate("qadashboard", testcase_name, None))
# item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
# item.setCheckState(QtCore.Qt.Unchecked)

This creates a list of test case name in the ListWidget. However I can't add make these items into checkboxes. item.setFlags or ItemIsUserCheckable is not recognised for list Widgets, so the two lines are commented out in the above example.

Any tips much appreciated.

wysota
19th March 2015, 20:49
You set flags on items, not on the widget. QListWidgetItem::setFlags() certainly exists and works.

pixxyman
20th March 2015, 11:31
Thanks, i achieved it with this:

for testcases in root.iter('testcase'):
testcase_name = QtGui.QApplication.translate(
"qadashboard", str(testcases.attrib), None)
item = QtGui.QListWidgetItem(testcase_name, self.listWidgetTestCases)
item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
item.setCheckState(QtCore.Qt.Unchecked)