我已经从一个文件填充了一个Qlistview,文件中的每一行都变成了一行。现在,我想有另一个函数来创建qlistview中所有选中项目的另一个文件。我的列表视图如下。如何获取Qlistview中的Checked项目?
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()
这是我迄今为止的尝试得到我想要的结果。不幸的是,它不打印我的检查项目。当被调用像这样self.print_checked_items(model)
我想知道什么可能是错的?
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()
你的意思是你想知道用户UN /检查列表中的项目时,或者更确切地说,查找被选中所有列表项? – Schollii
不,我希望在用户检查后从列表中选择所有已检查项目的列表。 – answerSeeker
编写一个查看列表项目并检查其选中状态的函数。发布代码,然后我们可以提供帮助。 – Schollii