PyQt - 通过隐藏行搜索 QTableView

5
这是我的问题。我有一个QTableView显示一些数据(在模型中设置),还有一个QLineEdit小部件,我想用它来搜索所有显示行中的文本。预期行为应该是:我在QLineEdit中输入一些文本,然后QTableView更新自己,隐藏不包含那些数据的所有行。
问题是,我应该如何实现这个功能?我找到了一个名为hideRows()的QTableView成员函数,似乎是正确的选择,但我无法弄清楚如何迭代所有数据以及将该方法放在哪里。它应该包含在模型或对话框内吗?(这实际上是我第一次使用模型,所以我只是理解了它们的工作原理)
另外,我需要实现一个导出函数(csv、html或其他格式),但只使用当前显示的行(那些没有隐藏的行)。这可能吗?
感谢您提供任何建议。
以下是我的代码:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import Android_extractor
import ui_android_dialog



class recordsTableModel(QAbstractTableModel):

    def __init__(self, records, parent = None):
        QAbstractTableModel.__init__(self, parent)
        self.__records = records

    def rowCount(self, parent):
        return len(self.__records)

    def columnCount(self, parent):
        return len(self.__records[0])

    def flags(self, index):
        return Qt.ItemIsEnabled | Qt.ItemIsSelectable

    def data(self, index, role):
        if role == Qt.EditRole:
            row = index.row()
            column = index.column()
            return self.__colors[row][column].name()

        if role == Qt.DisplayRole:
            row = index.row()
            column = index.column()
            value = self.__records[row][column]

            return value

    def headerData(self, section, orientation, role):
        if role == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return self.__records[0]._fields[section]




class AndroidDialog(QDialog, ui_android_dialog.Ui_androidDialog):

    def __init__(self, parent=None):
        super(AndroidDialog, self).__init__(parent)
        self.setupUi(self)

        self.buttonMapper = QSignalMapper(self)

        self.buttonMapper.setMapping(self.contactsToolButton, 0)
        self.buttonMapper.setMapping(self.groupsToolButton, 1)
        self.buttonMapper.setMapping(self.chatsessionToolButton, 2)
        self.buttonMapper.setMapping(self.messageToolButton, 3)

        self.contactsToolButton.clicked.connect(self.buttonMapper.map)
        self.groupsToolButton.clicked.connect(self.buttonMapper.map)
        self.chatsessionToolButton.clicked.connect(self.buttonMapper.map)
        self.messageToolButton.clicked.connect(self.buttonMapper.map)

        self.buttonMapper.mapped.connect(self.dataStackedWidget.setCurrentIndex)


        self.newQuery = Android_extractor.AndroidQuery(sys.argv[1])
        self.contacts = self.newQuery.getContacts()
        self.groups = self.newQuery.getGroups()
        self.chats = self.newQuery.getChats()

        self.contactsTableView.setModel(recordsTableModel(self.contacts))
        self.contactsTableView.resizeColumnsToContents()
        self.contactsTableView.resizeRowsToContents()
        self.groupsTableView.setModel(recordsTableModel(self.groups))
        self.groupsTableView.resizeColumnsToContents()
        self.groupsTableView.resizeRowsToContents()
        self.chatsessionTableView.setModel(recordsTableModel(self.chats))
        self.chatsessionTableView.resizeColumnsToContents()
        self.chatsessionTableView.resizeRowsToContents()



app = QApplication(sys.argv)
form = AndroidDialog()
form.show()
app.exec_()

你应该看一下 QSortFilterProxyModel 类。 - Dimitry Ernot
1个回答

6
你应该看看 QSortFilterProxyModel
不要直接将自定义模型设置在表视图上,而是将其设置为代理的源模型,然后将代理模型设置在你的视图上。
    self.proxyModelContact = QSortFilterProxyModel(self)
    self.proxyModelContact.setSourceModel(recordsTableModel(self.contacts))
    self.contactsTableView.setModel(self.proxyModelContact)

QSortFilterProxyModel提供了两个方法:

  • setFilterRegExp(pattern)允许您在视图上设置一个正则表达式过滤器(即只显示与模式匹配的项目)

  • setFilterKeyColumn(index)允许您定义用于过滤的列(如果index = -1,则将查看所有列)。

您只需将您的linedit的textChanged信号链接到一个更新过滤器的槽中,例如:

def onTextChanged(self, text):
    self.proxyModelContact.setFilterRegExp(str(text))

非常感谢,我甚至不知道这个!它完美地适合! - Rigel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接