Want more features on Pastebin? Sign Up, it's FREE!
Guest

QDataWidget mapper example

By: AlexVhr on May 27th, 2013  |  syntax: Python  |  size: 2.48 KB  |  views: 72  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from PySide import QtGui, QtCore
  2. import sys
  3.  
  4.  
  5. class User():
  6.     def __init__(self, name, password):
  7.         self.name = name
  8.         self.password = password
  9.  
  10.  
  11. class MyTableModel(QtCore.QAbstractTableModel):
  12.  
  13.     def __init__(self, parent, rows, columns):
  14.         super().__init__(parent)
  15.         self.rows = rows
  16.         self.columns = columns
  17.  
  18.     def columnCount(self, index):
  19.         return len(self.columns)
  20.  
  21.     def rowCount(self, index):
  22.         return len(self.rows)
  23.  
  24.     def data(self, index, role):
  25.         if index.isValid():
  26.             if (role == QtCore.Qt.DisplayRole) or (role == QtCore.Qt.EditRole):
  27.                 attr_name = self.columns[index.column()]
  28.                 row = self.rows[index.row()]
  29.                 return getattr(row, attr_name)
  30.  
  31.     def flags(self, index):
  32.         if not index.isValid():
  33.             return QtCore.Qt.ItemIsEnabled
  34.         else:
  35.             return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable
  36.  
  37.     def setData(self, index, value, role=QtCore.Qt.EditRole):
  38.         if index.isValid() and role == QtCore.Qt.EditRole:
  39.             attr_name = self.columns[index.column()]
  40.             row = self.rows[index.row()]
  41.             setattr(row, attr_name, value)
  42.  
  43.  
  44. class MyWindow(QtGui.QWidget):
  45.     def __init__(self):
  46.         super(MyWindow, self).__init__()
  47.         self.setLayout(QtGui.QFormLayout(self))
  48.         self.user_edit = QtGui.QLineEdit(self)
  49.         self.password_edit = QtGui.QLineEdit(self)
  50.         self.mapper = QtGui.QDataWidgetMapper(self)
  51.  
  52.         user1 = User('Mary', '12345')
  53.         user2 = User('John', 'qwerty')
  54.  
  55.         rows = [user1, user2]
  56.         columns = ['name', 'password']
  57.  
  58.         self.model = MyTableModel(self, rows, columns)
  59.         self.mapper.setModel(self.model)
  60.         self.mapper.addMapping(self.user_edit, 0)
  61.         self.mapper.addMapping(self.password_edit, 1)
  62.         self.mapper.toFirst()
  63.  
  64.         self.layout().addRow("User name:", self.user_edit)
  65.         self.layout().addRow("Password:", self.password_edit)
  66.  
  67.         self.btnPrevious = QtGui.QPushButton("Previous", self)
  68.         self.btnNext = QtGui.QPushButton("Next",self)
  69.         self.layout().addWidget(self.btnPrevious)
  70.         self.layout().addWidget(self.btnNext)
  71.  
  72.         self.btnPrevious.clicked.connect(self.mapper.toPrevious)
  73.         self.btnNext.clicked.connect(self.mapper.toNext)
  74.  
  75. if __name__ == "__main__":
  76.     app = QtGui.QApplication(sys.argv)
  77.     win = MyWindow()
  78.     win.show()
  79.     app.exec_()
clone this paste RAW Paste Data