1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
import sys
from PyQt4 import QtCore, QtGui
class CheckBoxDelegate(QtGui.QItemDelegate):
def __init__(self, owner):
QtGui.QItemDelegate.__init__(self, owner)
def paint(self, painter, option, index):
# Get item data
value = index.data(QtCore.Qt.DisplayRole).toBool()
# fill style options with item data
style = QtGui.QApplication.style()
opt = QtGui.QStyleOptionButton()
opt.state |= QtGui.QStyle.State_On if value else QtGui.QStyle.State_Off
opt.state |= QtGui.QStyle.State_Enabled
opt.text = ""
opt.rect = option.rect
# draw item data as CheckBox
style.drawControl(QtGui.QStyle.CE_CheckBox, opt, painter)
#QtGui.QItemDelegate.paint(self, painter, option, index)
def createEditor(self, parent, option, index):
# create check box as our editor.
editor = QtGui.QCheckBox(parent)
editor.installEventFilter(self)
editor.setGeometry(4,4,16,16)
self.connect (editor, QtCore.SIGNAL('clicked()'), self.toggleActive)
return editor
def setEditorData(self, editor, index):
# set editor data
value = index.data(QtCore.Qt.DisplayRole).toBool()
editor.setChecked(value)
def setModelData(self,editor,model,index):
value = editor.checkState()
model.setData(index, QtCore.QVariant(value))
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
def toggleActive(self):
print "toggled"
class ComboBoxDelegate(QtGui.QItemDelegate):
def __init__(self, owner, itemslist):
QtGui.QItemDelegate.__init__(self, owner)
self.itemslist = itemslist
def paint(self, painter, option, index):
# Get Item Data
value = index.data(QtCore.Qt.DisplayRole).toInt()[0]
# fill style options with item data
style = QtGui.QApplication.style()
opt = QtGui.QStyleOptionComboBox()
opt.currentText = str(self.itemslist[value])
opt.rect = option.rect
# draw item data as ComboBox
style.drawComplexControl(QtGui.QStyle.CC_ComboBox, opt, painter)
def createEditor(self, parent, option, index):
# create the ProgressBar as our editor.
editor = QtGui.QComboBox(parent)
editor.addItems(self.itemslist)
editor.setCurrentIndex(0)
editor.installEventFilter(self)
return editor
def setEditorData(self, editor, index):
value = index.data(QtCore.Qt.DisplayRole).toInt()[0]
editor.setCurrentIndex(value)
def setModelData(self,editor,model,index):
value = editor.currentIndex()
model.setData(index, QtCore.QVariant(value))
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
class VarTableView(QtGui.QTableView):
def __init__(self):
QtGui.QTableView.__init__(self)
self.horizontalHeader().setStretchLastSection(True)
self.mdl = QtGui.QStandardItemModel(2, 5)
self.setModel(self.mdl)
self.setEditTriggers(QtGui.QAbstractItemView.CurrentChanged)
self.setItemDelegateForColumn(0,CheckBoxDelegate(self))
self.setItemDelegateForColumn(2,ComboBoxDelegate(self, ['item1','item2', 'item3']))
self.setItemDelegateForColumn(3,ComboBoxDelegate(self, ['test1','test2', 'test3', 'test4', 'test5']))
self.setColumnWidth(0, 16)
self.setColumnWidth(2, 64)
self.setColumnWidth(3, 64)
self.setWindowTitle("Variables Table View")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setStyle(QtGui.QStyleFactory.create("Plastique"))
tableView = VarTableView()
for row in range (2):
for column in range (5):
index = tableView.mdl.index(row, column, QtCore.QModelIndex())
if column == 0:
tableView.mdl.setData(index,QtCore.QVariant(True))
elif column == 2:
tableView.mdl.setData(index,QtCore.QVariant(0))
else:
tableView.mdl.setData(index,QtCore.QVariant("Text"))
tableView.setGeometry(500,500,500,200)
tableView.show()
sys.exit(app.exec_()) |
Partager