Skip to content

Instantly share code, notes, and snippets.

@ShikouYamaue
Last active April 20, 2019 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShikouYamaue/6983f985c9c49d20f0e302d968f90375 to your computer and use it in GitHub Desktop.
Save ShikouYamaue/6983f985c9c49d20f0e302d968f90375 to your computer and use it in GitHub Desktop.
QAbstractTableModel Sample
class SampleTableModel(QAbstractTableModel):
def __init__(self, data, parent=None, influences=None, vertices=None):
super(self.__class__, self).__init__(parent)
self._weight_data = data
self.v_header_list = vertices
self.h_header_list = influences
#ヘッダー設定をオーバーライド
def headerData(self, id, orientation, role):
if orientation == Qt.Horizontal:
if role == Qt.DisplayRole:
return self.h_header_list[id]
if orientation == Qt.Vertical:
if role == Qt.DisplayRole:
return self.v_header_list[id]
return None
#行数を返す、オーバーライド
def rowCount(self, parent=None):
return len(self._weight_data)
#列数を返す、オーバーライド
def columnCount(self, parent=None):
return len(self._weight_data[0])-1 if self.rowCount() else 0
#データ設定関数をオーバーライド、 ロールに応じて処理
#ここで表示の値やカラーがすべて扱われます
def data(self, index, role=Qt.DisplayRole):
row = index.row()
column = index.column()
if role == Qt.DisplayRole:
weight = self._weight_data[row][column]
return weight
elif role == Qt.TextAlignmentRole:
return Qt.AlignRight#右揃え
#データセットする関数をオーバライド
def setData(self, index, value, role=Qt.EditRole):
row = index.row()
column = index.column()
if role == Qt.EditRole:
self._weight_data[row][column] = float(value)
self.dataChanged.emit(index, index)#データ変更シグナルを送出
return True
else:
return False
# 各セルのインタラクション
def flags(self, index):
#インデックスによる選択可否等をつけたければここで条件分岐
return Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable
#指定セルのデータを取得
def get_data(self, index):
row = index.row()
column = index.column()
value = float(self._weight_data[row][column])
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment