PDA

View Full Version : Changed orientation of QTableView, rowCount not working



lrb
28th May 2014, 08:20
Hi,

I have a QTableView using a proxy model based on QSortFilterProxyModel to display a single row in a source model.
The source model has a tree structure:

Root
--Parent0
----Child0
----Child1
--Parent1
----Child2

To display the row for Child1, I set the table rootIndex to Parent0, and give the proxy model the row number of Child1.
The rowCount method returns 1 since I only want one row, while columnCount returns the number of columns in Child1.
The proxy model data method looks like this:



def data(self, index, role=Qt.DisplayRole):
row = self.current_row()
col = index.column()

if role == Qt.DisplayRole:
new_index = index.sibling(row, col)
return super(MyProxyModel, self).data(new_index, role)


This works fine normally. However, I need to present the single-row table as a single column for use as a properties widget.
To do this I swapped the values returned by rowCount and columnCount (i.e. columnCount now returns 1 while rowCount returns the number of columns in Child1), and changed data() to this:



def data(self, index, role=Qt.DisplayRole):
row = self.current_row()
col = index.row()

if role == Qt.DisplayRole:
new_index = index.sibling(row, col)
return super(MyProxyModel, self).data(new_index, role)


The table is drawn with the correct number of rows (e.g. 7 to match the number of columns in Child1), but it only fills data in the first few rows (e.g. 2 to match the number of children under Parent0).
It looks like QTableView is using the row count of its rootIndex instead of model.rowCount, so it only calls model.data on that many rows.
Is there a fix for this?

lrb
29th May 2014, 08:16
Okay I figured it out. QTableView calls proxy_model.data() using an index, which it gets from proxy_model.index().
I needed to reimplement proxy_model.index() to swap the row/column in the request, otherwise the table would be requesting rows and columns that don't exist in the source model. The proxy_model.data method now looks like this:



def data(self, index, role=Qt.DisplayRole):
row = self.current_row()
col = index.column()

if role == Qt.DisplayRole:
new_index = super(MyProxyModel, self).index(col, row, index.parent())
return super(MyProxyModel, self).data(new_index, role)


I had to change the "new_index = index.sibling(row, col)" to a call to super.index since the reimplemented version of index() would give the wrong result when used to get siblings.

Anyway it works now.