Results 1 to 5 of 5

Thread: Using model with different views

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Join Date
    Apr 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Using model with different views

    I've come up with a solution using a QIdentityProxyModel for the bottom view and just using the main model for the top view. I connect the currentRowChanged() signal from the top views selection model to a slot that simply does the following:

    Qt Code:
    1. def updateProxyModel(current, previous)
    2. ProxyModel.beginResetModel()
    3. ProxyModel.selectedRow = current.row()
    4. ProxyModel.endResetModel()
    To copy to clipboard, switch view to plain text mode 

    I did have to reimplement index() as the base implementation of QAbstractTableModel calls hasIndex() which checks the rowCount() and columnCount() of only the main model, and would never generate indexes above 2 columns for the proxy model. But this was ok, because I like to use internalPointer() anyway to save on a little bit of typing in the data() methods

    Main model:

    Qt Code:
    1. class MainModel(QAbstractTableModel):
    2. def __init__(self, data, parent=None):
    3. super(MainModel, self).__init__(parent)
    4. self.headers = ('Name', 'Value')
    5. self.data = data
    6.  
    7. def columnCount(self, parent=QModelIndex()):
    8. return len(self.headers)
    9.  
    10. def rowCount(self, parent=QModelIndex()):
    11. return len(self.data)
    12.  
    13. def index(self, row, col, parent=None):
    14. return self.createIndex(row, col, self.data[row])
    15.  
    16. def flags(self, index):
    17. return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemNeverHasChildren
    18.  
    19. def headerData(self, column, orientation, role):
    20. if orientation == Qt.Horizontal and role == Qt.DisplayRole:
    21. return self.headers[column]
    22.  
    23. def data(self, index, role=Qt.DisplayRole):
    24. if not index.isValid():
    25. return None
    26.  
    27. rowData = index.internalPointer()
    28. col = index.column()
    29.  
    30. if role == Qt.DisplayRole:
    31. if col == 0:
    32. return rowData.name
    33. if col == 1:
    34. return rowData.value
    35.  
    36. return None
    To copy to clipboard, switch view to plain text mode 

    Proxy Model:

    Qt Code:
    1. class ProxyModel(QIdentityProxyModel):
    2. def __init__(self, data, parent=None):
    3. super(ProxyModel, self).__init__(parent)
    4. self.data = data
    5. self.headers = ('Attribute', 'Width', 'Height', 'Pos X', 'Pos Y')
    6. self.selectedRow = None
    7.  
    8. def headerData(self, column, orientation, role=None):
    9. if orientation == Qt.Horizontal and role == Qt.DisplayRole:
    10. return self.headers[column]
    11.  
    12. def columnCount(self, parent=QModelIndex()):
    13. if parent.isValid():
    14. return 0
    15.  
    16. return len(self.headers)
    17.  
    18. def rowCount(self, parent=QModelIndex()):
    19. if parent.isValid() or not self.selectedRow:
    20. return 0
    21.  
    22. return len(self.data[self.selectedRow].attributes)
    23.  
    24. def data(self, index, role=Qt.DisplayRole):
    25. if not index.isValid() or not self.selectedRow:
    26. return None
    27.  
    28. col = index.column()
    29. row = index.row()
    30.  
    31. if role == Qt.DisplayRole:
    32. return self.data[self.selectedRow].attributes[row][col]
    33.  
    34. return None
    To copy to clipboard, switch view to plain text mode 

    Decent solution? What could be done better or differently?
    Last edited by Wallabee19; 25th April 2017 at 10:31.

Similar Threads

  1. Same model different views QTableView
    By Jarrod in forum Newbie
    Replies: 1
    Last Post: 12th August 2016, 05:49
  2. Model with two very different views
    By mholmes in forum Qt Programming
    Replies: 3
    Last Post: 7th April 2010, 23:19
  3. how to disable views from a model
    By zeeeend in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2008, 21:14
  4. 2 Views 1 Model
    By PrimeCP in forum Qt Programming
    Replies: 3
    Last Post: 2nd October 2007, 01:40
  5. Model/Views Woes
    By Scorp1us in forum Qt Programming
    Replies: 3
    Last Post: 8th February 2007, 03:10

Tags for this Thread

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.