Advertisement
jkrieger

TransposeModel

Apr 8th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. #-*- coding: utf-8 -*-
  2.  
  3. import os
  4. import sys
  5.  
  6. # Python versions before 3.0 do not use UTF-8 encoding
  7. # by default. To ensure that Unicode is handled properly
  8. # throughout the program, we will set the default encoding
  9. # ourselves to UTF-8.
  10. reload(sys)
  11. sys.setdefaultencoding('utf-8')
  12.  
  13.  
  14. from PyQt4 import QtGui
  15. from PyQt4 import QtCore
  16. from PyQt4 import uic
  17.  
  18.  
  19.  
  20. class TransposeProxyModel(QtGui.QAbstractProxyModel):
  21.  
  22.     def __init__(self, *argv):
  23.  
  24.         super(TransposeProxyModel, self).__init__(*argv)
  25.  
  26.  
  27.     def mapFromSource (self, sourceIndex):
  28.  
  29.         return self.index(sourceIndex.column(), sourceIndex.row())
  30.  
  31.  
  32.     def mapToSource (self, proxyIndex):
  33.        
  34.         return self.sourceModel().index(proxyIndex.column(), proxyIndex.row())
  35.  
  36.  
  37.     def index (self, row, column, parent=QtCore.QModelIndex()):
  38.  
  39.         index = self.createIndex(row, column)
  40.         print index.isValid()
  41.  
  42.         return index
  43.  
  44.  
  45.     def parent(self, childIndex):
  46.  
  47.         return QtCore.QModelIndex()
  48.  
  49.  
  50.     # def flags(self, index):
  51.  
  52.     #   return self.sourceModel().flags(self.mapToSource(index))
  53.  
  54.  
  55.     def rowCount(self, parent=QtCore.QModelIndex()):
  56.  
  57.         return self.sourceModel().columnCount()
  58.  
  59.  
  60.     def columnCount(self, parent=QtCore.QModelIndex()):
  61.  
  62.         return self.sourceModel().rowCount()
  63.  
  64.  
  65.     def data(self, index, role):
  66.  
  67.         return self.sourceModel().data(self.mapToSource(index), role)
  68.  
  69.  
  70.  
  71. class PyQtApp(QtGui.QApplication):
  72.  
  73.     def __init__(self, *argv):
  74.  
  75.         super(PyQtApp, self).__init__(*argv)
  76.  
  77.         self.ui = QtGui.QMainWindow()
  78.         uic.loadUi('window.ui', self.ui)
  79.  
  80.         # models
  81.         self.mBase = QtGui.QStandardItemModel()
  82.  
  83.         self.mTransponsed = TransposeProxyModel()
  84.         # self.mTransponsed = QtGui.QSortFilterProxyModel()
  85.         self.mTransponsed.setSourceModel(self.mBase)
  86.  
  87.         # widgets
  88.         self.ui.baseView.setModel(self.mBase)
  89.         self.ui.tableView.setModel(self.mBase)
  90.         self.ui.transponsedView.setModel(self.mTransponsed)
  91.  
  92.         self.ui.loadButton.clicked.connect(self.on_loadButton_clicked)
  93.  
  94.  
  95.  
  96.     def run (self):
  97.  
  98.         self.ui.show()
  99.         self.exec_()
  100.  
  101.  
  102.     def on_loadButton_clicked(self):
  103.  
  104.         attributes = {'1st': '1val', '2nd': '2val'}
  105.        
  106.         for attr in attributes:
  107.             item = QtGui.QStandardItem(attr)
  108.             self.mBase.appendRow(item)
  109.  
  110.  
  111.  
  112. if __name__ == '__main__':
  113.  
  114.     i = PyQtApp(sys.argv)
  115.     i.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement