Guest User

SublimeText + Arch, testing program

a guest
Jan 28th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. from PyQt5.QtCore import *
  2. from PyQt5.QtGui import *
  3. from PyQt5.QtWidgets import *
  4. import sys
  5.  
  6.  
  7. class My_Model_table(QAbstractTableModel):
  8.     def __init__(self, table_data=[], parent=None):
  9.         super().__init__()
  10.         self.table_data = table_data
  11.  
  12.     def rowCount(self, parent):
  13.         return len(self.table_data)
  14.  
  15.     def columnCount(self, parent):
  16.         return 1
  17.  
  18.     def data(self, index, role):
  19.         if role == Qt.DisplayRole:
  20.             value = self.table_data[index.row()]
  21.             return value
  22.         if role == Qt.TextAlignmentRole:
  23.             return Qt.AlignCenter
  24.  
  25.         if role == Qt.BackgroundRole:
  26.             if index.row() % 2 == 0:
  27.                 return QBrush(Qt.green)
  28.  
  29.     def setData(self, row, value):
  30.         print(row)
  31.         print(value)
  32.         self.table_data[row][1] = value
  33.         return True
  34.  
  35. class My_table(QTableView):
  36.     def __init__(self, parent=None):
  37.         super().__init__()
  38.         self.activated.connect(self.double_click_enter)
  39.  
  40.     def double_click_enter(self, QModelIndex):
  41.         row = QModelIndex.row()
  42.         QModelIndex.model().setData(row, 'red')
  43.  
  44.  
  45. if __name__ == '__main__':
  46.     app = QApplication(sys.argv)
  47.     data = ['1', '2', '3', '4', '5']
  48.     main_table = My_table()
  49.     main_table.setModel(My_Model_table(data))
  50.  
  51.     main_table.show()
  52.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment