Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Last active August 24, 2020 04:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyllanesc/a4caf9b474d8e0c21df3a7d3c2a54a2c to your computer and use it in GitHub Desktop.
Save eyllanesc/a4caf9b474d8e0c21df3a7d3c2a54a2c to your computer and use it in GitHub Desktop.
from PySide2.QtWidgets import *
from PySide2.QtCore import *
class Model(QAbstractTableModel):
def __init__(self, cycles = [[]], headers = [], parent = None):
QAbstractTableModel.__init__(self, parent)
self.cycles = cycles
self.headers = headers
self.values_checked = []
def rowCount(self, parent):
return len(self.cycles)
def columnCount(self, parent):
return len(self.cycles[0])
def flags(self, index):
fl = Qt.ItemIsEnabled | Qt.ItemIsSelectable
if index.column() == 0:
fl |= Qt.ItemIsUserCheckable
else:
fl |= Qt.ItemIsEditable
return fl
def data(self, index, role):
if not index.isValid():
return
row = index.row()
column = index.column()
if role == Qt.DisplayRole:
value = self.cycles[row][column]
return value
elif role == Qt.TextAlignmentRole:
return Qt.AlignCenter;
elif role == Qt.CheckStateRole and column==0:
return Qt.Checked if self.cycles[row][column] else Qt.Unchecked
def setData(self, index, value, role = Qt.EditRole):
change = False
row = index.row()
column = index.column()
if role == Qt.CheckStateRole:
value = value != Qt.Unchecked
change = True
if role == Qt.EditRole:
if (column == 1) or (column == 4):
try:
str(value)
change = True
except:
pm.warning("Not a valid name")
change = False
elif (column == 2):
try:
int(value)
change = True
except:
pm.warning("Not a valid frame")
change = False
elif (column == 3):
try:
int(value)
change = True
except:
pm.warning("Not a valid frame")
change = False
elif (column == 5):
try:
int(value)
change = True
except:
pm.warning("Not a valid version number")
change = False
if change:
self.cycles[row][column] = value
self.dataChanged.emit(row, column)
return True
return False
def headerData(self, section, orientation, role):
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return self.headers[section]
def insertRows(self, position, rows, values = [] , parent =QModelIndex()):
self.beginInsertRows(parent, position, position+rows-1)
self.cycles.insert(position, values)
self.endInsertRows()
self.getData()
def roleNames(self):
roles = QAbstractTableModel.roleNames(self)
roles["Checked"] = Qt.CheckStateRole
return roles
def getData(self):
rows = self.rowCount(1)
data = []
for row in range(rows):
array = []
for column in range (6):
index = self.index(row, column)
info = index.data()
array.append(info)
data.append(array)
dic = {}
for item in data:
dic[item[1]]=item
print("")
print("data:")
print('')
for key in dic:
print(key, dic[key])
class EmptyDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
opt = QStyleOptionViewItem(option)
self.initStyleOption(opt, index)
opt.text = ""
QApplication.style().drawControl(QStyle.CE_ItemViewItem, opt, painter)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = QTableView()
w.setItemDelegateForColumn(0, EmptyDelegate(w))
headers = ["Select", " Cycle Name ", " Start ", " End ", "Info", "Version", " Del "]
cycles = [[True,"idle","1","70","cool information","0", "deleteBtnPlaceHolder"]]
model = Model(cycles, headers)
w.setModel(model)
w.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment