本文介绍了如何在 Qlistview 中获取选中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

到目前为止,这是我获得理想结果的尝试.不幸的是,它不打印我检查的项目.当这样调用 self.print_checked_items(model) 时,我想知道有什么问题?

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

推荐答案

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

这篇关于如何在 Qlistview 中获取选中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 01:35