标签:
#!/usr/bin/python
#-*-coding: utf-8 -*-
from PyQt4.QtGui import (QApplication, QHeaderView, QItemSelectionModel,
QStandardItemModel,QTableView,QCheckBox,QComboBox,
QItemDelegate,QStyleOptionViewItem,QSpinBox, QTreeView)
from PyQt4.QtCore import QModelIndex, QVariant, Qt
class ComboBoxDelegate(QItemDelegate):
def __init__(self, parent=None):
super(ComboBoxDelegate,self).__init__(parent)
def createEditor(self,parent,option,index):
combo = QComboBox(parent)
combo.addItems([unicode(index.row()),unicode(index.column())])
return combo
def setEditorData(self, editor,index):
data = index.model().itemFromIndex(index).text()
combo_box_index = editor.findText(data)
print combo_box_index
if combo_box_index >=0 :
editor.setCurrentIndex(combo_box_index)
def setModelData(self, editor,model,index):
text = editor.currentText()
model.setData(index, text, Qt.EditRole)
import sys
if __name__==‘__main__‘:
app = QApplication(sys.argv)
model=QStandardItemModel(4, 3)
tableView=QTableView()
tableView.setModel(model)
delegate = ComboBoxDelegate()
tableView.setItemDelegate(delegate)
# tableView.horizontalHeader().setStretchLastSection(True)
for row in range(4):
for column in range(3):
index = model.index(row, column, QModelIndex())
model.setData(index, QVariant((row+1) * (column+1)))
model.setData(index, QVariant([u‘zéro‘,u‘un‘,u‘deux‘,u‘trois‘]))
tableView.setWindowTitle("Delegate")
tableView.show()
app.exec_()
标签:
原文地址:http://www.cnblogs.com/mhxy13867806343/p/4300004.html