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)
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)
To copy to clipboard, switch view to plain text mode
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)
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)
To copy to clipboard, switch view to plain text mode
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?
Bookmarks