Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt5.QtCore import *
- from PyQt5.QtGui import *
- from PyQt5.QtWidgets import *
- import sys
- class My_Model_table(QAbstractTableModel):
- def __init__(self, table_data=[], parent=None):
- super().__init__()
- self.table_data = table_data
- def rowCount(self, parent):
- return len(self.table_data)
- def columnCount(self, parent):
- return 1
- def data(self, index, role):
- if role == Qt.DisplayRole:
- value = self.table_data[index.row()]
- return value
- if role == Qt.TextAlignmentRole:
- return Qt.AlignCenter
- if role == Qt.BackgroundRole:
- if index.row() % 2 == 0:
- return QBrush(Qt.green)
- def setData(self, row, value):
- print(row)
- print(value)
- self.table_data[row][1] = value
- return True
- class My_table(QTableView):
- def __init__(self, parent=None):
- super().__init__()
- self.activated.connect(self.double_click_enter)
- def double_click_enter(self, QModelIndex):
- row = QModelIndex.row()
- QModelIndex.model().setData(row, 'red')
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- data = ['1', '2', '3', '4', '5']
- main_table = My_table()
- main_table.setModel(My_Model_table(data))
- main_table.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment