I've populated a Qlistview from a file, each line in the file becomes a row. Now, I'd like to have another function that creates another file from all the checked items in the qlistview. My listview goes as follows.
def show_list(self, file_in):
QListView.__init__(self)
QListView.setWindowFlags(self, QtCore.Qt.WindowStaysOnTopHint)
QListView.setWindowTitle(self, "ListView")
self.buttonBox = QtGui.QDialogButtonBox(self)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
list_view = QListView(self)
list_view.setMinimumSize(350,350)
self.verticalLayout = QtGui.QVBoxLayout(self)
self.verticalLayout.addWidget(list_view)
self.verticalLayout.addWidget(self.buttonBox)
self.buttonBox.accepted.connect(self.close)
self.buttonBox.rejected.connect(self.close)
model = QStandardItemModel(list_view)
with open(file_in) as f:
if f is not None:
item = f.readlines()
for line in item:
item = QStandardItem(line)
item.setCheckable(True)
item.setCheckState(QtCore.Qt.Unchecked)
model.appendRow(item)
list_view.setModel(model)
list_view.show()
This is my attempt so far to get my desired result. Unfortunately, It doesn't print my checked items. When called like this self.print_checked_items(model)
I'm wondering what could be wrong?
def print_checked_items(self, model):
path = "/home/test1/checked.txt"
for index in range(model.rowCount()):
item = model.item(index)
if item.isCheckable() and item.checkState() == QtCore.Qt.Checked:
with open(path, "a") as f_out:
print ('%s\n' % item.text())
f_out.write('%s\n' % item.text()