简体   繁体   English

QSortFilterProxyModel获取过滤数据

[英]QSortFilterProxyModel get Filtered Data

I'm pretty inexperienced with this kind of object in Qt and i need to know if there is a way to retrieve the data after filtering (for doing something with them, for example export in another file). 我对Qt中的这种对象缺乏经验,我需要知道是否有一种方法可以在过滤后检索数据(对它们进行处理,例如导出到另一个文件中)。

The situation is like this, i get data from a database and store it in a python list of list, after that i create a QTableView model and initialize it with a QSortFilterProxyModel set up for containing this data. 情况是这样的,我从数据库获取数据并将其存储在列表的python列表中,此后,我创建了QTableView模型,并使用设置为包含此数据的QSortFilterProxyModel对其进行了初始化。 In the interface there is a QLineEdit connected to the setFilterRegExp method, whose purpose is for searching through the data in the QTableView. 在该接口中,有一个QLineEdit连接到setFilterRegExp方法,其目的是在QTableView中搜索数据。

I need to create a button (or whatever) that writes a file with the data currently displayed on the GUI but i cannot figure out how to retrieve the currently displayed data. 我需要创建一个按钮(或其他任何按钮),以使用GUI上当前显示的数据写入文件,但是我无法弄清楚如何检索当前显示的数据。

Thank you for any advice. 感谢您的任何建议。

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.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.proxyModelContact = QSortFilterProxyModel(self)
    self.proxyModelContact.setSourceModel(recordsTableModel(self.contacts))
    self.proxyModelContact.setFilterKeyColumn(-1)
    self.proxyModelContact.setFilterCaseSensitivity(Qt.CaseInsensitive)
    self.contactsTableView.setModel(self.proxyModelContact)

    self.contactsExportToolButton.clicked.connect(self.printData)

def printData(self):
    print "%s" % self.proxyModelContact.rowCount()
    print "%s" % self.proxyModelContact.data(self.proxyModelContact.index(0, 0))

for what i know the index should point at the item in the model (for me a table) so with this it should print the first item in the first column. 对于我所知道的索引应该指向模型中的项(对我来说是一张表),因此它应该在第一列中打印第一项。 Insteat it prints: 实例打印:
PyQt4.QtCore.QVariant object at 0x02F7B030 PyQt4.QtCore.QVariant对象位于0x02F7B030

You can use rowCount , columnCount and data methods of the model attached to the view to access displayed data. 您可以使用视图附带的模型的rowCountcolumnCountdata方法访问显示的数据。 In your case the model is a QSortFilterProxyModel . 在您的情况下,模型是QSortFilterProxyModel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 QSortFilterProxyModel - 获取过滤项的数量 - QSortFilterProxyModel - get the number of filtered items 如何在 QSortFilterProxyModel 中获取过滤的 rowCount - How to get filtered rowCount in a QSortFilterProxyModel 如何通过 QSortFilterProxyModel 在 QAbstractItemModel 中插入行数据? - How to insert row data in QAbstractItemModel through a QSortFilterProxyModel? Django根据G​​ET请求从数据库中查询过滤的数据 - Django query filtered data from database based on GET request 如何在Django中使用多个外键联接表并获取过滤后的数据? - How to join tables with multiple foreign keys and get filtered data in Django? 从 Google 表格 API 和 python 获取过滤后的数据? - Get filtered data from Google Sheets API with python? Django通过一个表获取数据,并使用过滤的相关对象集 - Django get data by one table with filtered set of related objects 如何使用filter()获取过滤数据的位置而不是值? - How to use filter() to get the location of the filtered data instead of the value? 获取子列的平均值,但来自 SQLAlchemy 中过滤的父数据 - Get average of a child column, but from filtered parent data in SQLAlchemy Qt 和 Python - QIdentityProxyModel 在嵌套在 QSortFilterProxyModel 顶部时没有获得正确的列数 - Qt and Python - QIdentityProxyModel does not get the right column count when nested on top of a QSortFilterProxyModel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM