Skip to main content
 首页 » 编程设计

python之如何在 Qlistview 中获取选中的项目

2025年05月04日14lautakyan007

我从一个文件中填充了一个 Qlistview,文件中的每一行都变成了一行。现在,我想要另一个函数来从 qlistview 中的所有选中项目创建另一个文件。我的 ListView 如下。

 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() 

请您参考如下方法:

当我在 Python 3.5 和 PyQt5 中运行它时,它工作正常,它打印正确的模式和勾选的项目。我删除了文件读取/写入行以进行测试。对于 PyQt4 和 Python 2.7,您应该只需要修复几个导入和打印语句。运行它,勾选几个项目,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)