분명히 편집기 역할을 위임하는 것이 좋습니다. 절단과 내가 찾은 예를 붙여 편집 한 후, 내가 가지고 뭔가 작업 : 이제
import sys
from PyQt4 import QtGui, QtCore
class MyModel(QtGui.QStandardItemModel):
def __init__(self):
super(QtGui.QStandardItemModel, self).__init__()
self.setColumnCount(3)
self.setHorizontalHeaderLabels(['Col 1', 'Col2 2', 'Col 3'])
def setData(self, index, value, role=QtCore.Qt.DisplayRole):
item = self.itemFromIndex(index)
item.setData(value, role=QtCore.Qt.DisplayRole)
class ComboDelegate(QtGui.QItemDelegate):
def __init__(self, parent):
QtGui.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
combo = QtGui.QComboBox(parent)
combo.addItem('a')
combo.addItem('b')
combo.addItem('c')
combo.addItem('t')
combo.addItem('w')
return combo
def setEditorData(self, editor, index):
text = index.data().toString()
index = editor.findText(text)
editor.setCurrentIndex(index)
def setModelData(self, editor, model, index):
model.setData(index, editor.itemText(editor.currentIndex()))
def updateEditorGeometry(self, editor, option, index):
print option, option.rect
editor.setGeometry(option.rect)
def row_clicked(model_index):
row = model_index.row()
print model_index.data(0).toString()
if __name__ == '__main__':
myapp = QtGui.QApplication(sys.argv)
model = MyModel()
view = QtGui.QTreeView()
view.setUniformRowHeights(True)
view.setModel(model)
view.setItemDelegateForColumn(1, ComboDelegate(view))
view.show()
view.pressed.connect(row_clicked)
for n in [['q','w','e'],['r','t','y']]:
item_a = QtGui.QStandardItem(unicode(n[0]))
item_b = QtGui.QStandardItem(unicode(n[1]))
item_c = QtGui.QStandardItem(unicode(n[2]))
model.appendRow([item_a, item_b, item_c])
for row in range(0, model.rowCount()):
view.openPersistentEditor(model.index(row, 1))
myapp.exec_()
는 문제는 내가 모델 항목의 데이터를 변경하는 콤보 박스를 사용하고, 행 창의 크기를 조정할 때까지 뷰 행의 높이가 콤보의 높이와 일치하지 않습니다. 뭐가 문제 야?
출처
2014-11-09 18:33:21
JWA