Search code examples
pythonqtpyqtqtableview

How do I get the entire selected row in a Qt proxy model?


The code below is a working QTableView, which is using a QAbstractTableModel, which is using a QSortFilterProxyModel. I've managed to figure out how to get data out of a single cell of a selected row, but not the entire row at once (e.g. as a list of strings). Please can I have some suggestions of what to try?

(I'm using Python, but if someone knows how to do it in another language I'll try to translate it...)

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys


class CustomTableModel(QAbstractTableModel):
    def __init__(self, cells=[[]], headers=[]):
        super(CustomTableModel, self).__init__()
        self._cells = cells
        self._headers = headers

    def data(self, index, role):
        if index.isValid() and (role == Qt.DisplayRole):
            return self._cells[index.row()][index.column()]

    def rowCount(self, parent=None):
        return len(self._cells)

    def columnCount(self, parent=None):
        return len(self._headers)

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


class CustomSortFilterProxyModel(QSortFilterProxyModel):
    def __init__(self):
        super(CustomSortFilterProxyModel, self).__init__()

    def get_selected_row(self):
        # Unsure what to put here??
        pass


def table_clicked():
    selected_indexes = table_view.selectionModel().selectedRows()
    first_cell_selected = proxy_model.data(proxy_model.index(selected_indexes[0].row(), 0), Qt.DisplayRole).toString()
    print(first_cell_selected)

    # But rather than the above I would like to be able to do something like:
    print(proxy_model.get_selected_row())
    # and for it to print out everything in the row e.g. ['Cell 1', 'Cell 2', 'Cell 3']


app = QApplication(sys.argv)

table_data = [["Cell 1", "Cell 2", "Cell 3"], ["Cell 4", "Cell 5", "Cell 6"]]
table_headers = ["Header 1", "Header 2", "Header 3"]

model = CustomTableModel(table_data, table_headers)

proxy_model = CustomSortFilterProxyModel()
proxy_model.setDynamicSortFilter(True)
proxy_model.setSourceModel(model)

table_view = QTableView()
table_view.setModel(proxy_model)
table_view.setSelectionBehavior(QAbstractItemView.SelectRows)
table_view.setSelectionMode(QAbstractItemView.SingleSelection)
table_view.setSortingEnabled(True)
table_view.clicked.connect(table_clicked)
table_view.show()

sys.exit(app.exec_())

It might be related to How to get Index Row number from Source Model


Solution

  • There's no ready-made method to get the data of the full row. You'll have to loop over the existing columns.

    So basically, you can transform your existing line:

    first_cell_selected = proxy_model.data(proxy_model.index(selected_indexes[0].row(), 0), Qt.DisplayRole).toString()
    

    into a list comprehension to get the content of each individual cell:

    row = selected_indexes[0].row()
    row_data = [proxy_model.index(row, col).data().toString()
                for col in xrange(proxy_model.columnCount())]
    

    By the way: A QModelIndex also offers a data() method and Qt.DisplayRole is the default role here. So you can simplify

    proxy_model.data(proxy_model.index(row, col), Qt.DisplayRole)
    

    to

    proxy_model.index(row, col).data()
    

    which somewhat easier to read.