我已经从文件填充了Qlistview,文件中的每一行都变成了一行。现在,我想要另一个函数从qlistview中的所有已检查项创建另一个文件。我的列表视图如下。
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()
答案 0 :(得分:4)
当我在Python 3.5和PyQt5中运行它时,它工作正常,它打印正确的模式和选中标记的项目。我删除了文件读/写行以进行测试。对于PyQt4和Python 2.7,您只需要修复几个导入和print语句。运行它,勾选几个项目,5秒后你在控制台中看到了什么?
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QWizardPage, QListView
class AppRemovalPage(QWizardPage):
def __init__( self, parent ):
super(AppRemovalPage, self).__init__(parent)
self.setTitle('Apps to Remove')
self.setSubTitle('Listview')
self.list_view = QListView(self)
self.list_view.setMinimumSize(465, 200)
self.isWritten = False
loo = "/home/test1/file.txt"
self.model = QtGui.QStandardItemModel(self.list_view)
for line in ('a', 'b', 'c', 'd', 'e'):
self.item = QtGui.QStandardItem(line)
self.item.setCheckable(True)
self.item.setCheckState(QtCore.Qt.Unchecked)
self.model.appendRow(self.item)
self.list_view.setModel(self.model)
self.list_view.show()
def print_checked_items(self):
for index in range(self.model.rowCount()):
item = self.model.item(index)
if item.checkState() == QtCore.Qt.Checked:
if self.isWritten:
mode = "a"
else:
mode = "w"
self.isWritten = True
print ('%s' % item.text())
print("print checked items executed")
app = QApplication([])
listview = AppRemovalPage(None)
listview.show()
QTimer.singleShot(5000, listview.print_checked_items)
app.exec_()
如果我勾选a,c和d,我会看到:
a w
c a
d a
print checked items executed
更新以显示Python文件对象如何工作(实际上是更好的代码,因为它支持使用上下文管理):
def print_checked_items(self):
path = "checked.txt"
mode = 'a' if self.isWritten else 'w'
if len(self.items) > 0:
with open(path, mode) as file:
for item in self.items:
print('%s' % item.text())
file.write(item.text() + "\n")
file.close()
print("print checked items executed")
连接可以写成wizard.button(QWizard.NextButton).clicked.connect(appremoval.print_checked_items)
答案 1 :(得分:3)
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
class AppRemovalPage(QtGui.QWizardPage):
def __init__(self, parent=None):
super(AppRemovalPage, self).__init__(parent=parent)
self.setTitle('Apps to Remove')
self.setSubTitle('Listview')
self.list_view = QtGui.QListView(self)
self.list_view.setMinimumSize(465, 200)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.list_view)
self.setLayout(layout)
self.items = []
self.isWritten = False
loo = "/home/test1/file.txt"
self.model = QtGui.QStandardItemModel(self.list_view)
self.model.itemChanged.connect(self.setItems)
file = QtCore.QFile(loo)
if file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text):
while not file.atEnd():
line = bytearray(file.readLine()).decode().strip()
item = QtGui.QStandardItem(line)
item.setCheckable(True)
item.setCheckState(QtCore.Qt.Unchecked)
self.model.appendRow(item)
self.list_view.setModel(self.model)
self.list_view.show()
def setItems(self, item):
if item.checkState() == QtCore.Qt.Checked:
self.items.append(item)
if item.checkState() == QtCore.Qt.Unchecked:
self.items.remove(item)
def print_checked_items(self):
path = "/home/test1/checked.txt"
mode = QtCore.QFile.Append if self.isWritten else QtCore.QFile.WriteOnly
if len(self.items) > 0:
file = QtCore.QFile(path)
if file.open(mode):
for item in self.items:
print('%s' % item.text())
file.write(item.text() + "\n")
file.close()
print("print checked items executed")
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
wizard = QtGui.QWizard()
appremoval = AppRemovalPage()
wizard.addPage(appremoval)
wizard.addPage(QtGui.QWizardPage())
wizard.button(QtGui.QWizard.NextButton).clicked.connect(appremoval.print_checked_items)
wizard.show()
sys.exit(app.exec_())
输出:
a
d
e
print checked items executed