[英]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. 您可以使用视图附带的模型的
rowCount
, columnCount
和data
方法访问显示的数据。 In your case the model is a QSortFilterProxyModel
. 在您的情况下,模型是
QSortFilterProxyModel
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.