Results 1 to 2 of 2

Thread: Changed orientation of QTableView, rowCount not working

  1. #1
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Question Changed orientation of QTableView, rowCount not working

    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:

    Qt Code:
    1. def data(self, index, role=Qt.DisplayRole):
    2. row = self.current_row()
    3. col = index.column()
    4.  
    5. if role == Qt.DisplayRole:
    6. new_index = index.sibling(row, col)
    7. 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:

    Qt Code:
    1. def data(self, index, role=Qt.DisplayRole):
    2. row = self.current_row()
    3. col = index.row()
    4.  
    5. if role == Qt.DisplayRole:
    6. new_index = index.sibling(row, col)
    7. 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?

  2. #2
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Changed orientation of QTableView, rowCount not working

    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:

    Qt Code:
    1. def data(self, index, role=Qt.DisplayRole):
    2. row = self.current_row()
    3. col = index.column()
    4.  
    5. if role == Qt.DisplayRole:
    6. new_index = super(MyProxyModel, self).index(col, row, index.parent())
    7. return super(MyProxyModel, self).data(new_index, role)
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by lrb; 29th May 2014 at 07:18. Reason: reformatted to look better

Similar Threads

  1. Detect when the content of a cell of QTableView is changed
    By qt_developer in forum Qt Programming
    Replies: 5
    Last Post: 21st August 2021, 16:00
  2. Change QTableView orientation
    By r2d2u2 in forum Qt Programming
    Replies: 2
    Last Post: 31st May 2010, 10:30
  3. Value changed in a QTableView field
    By nittalope in forum Newbie
    Replies: 4
    Last Post: 12th August 2009, 09:21
  4. Index (position) changed on saving with QTableView
    By Auryn in forum Qt Programming
    Replies: 2
    Last Post: 22nd July 2008, 08:15
  5. Replies: 9
    Last Post: 23rd November 2006, 11:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.