简体   繁体   中英

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

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. In the interface there is a QLineEdit connected to the setFilterRegExp method, whose purpose is for searching through the data in the 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.

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

You can use rowCount , columnCount and data methods of the model attached to the view to access displayed data. In your case the model is a QSortFilterProxyModel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM