カスタムデリゲートを使用して、QTableViewのコンボボックスの列を表示します。 値はすべてのコンボボックスで同じですので、実際には問題のある人口部分ではありません。QTableViewのカスタムデリゲートのcomboBoxの選択項目
私はそれらを選択したアイテムとして表示したい、データベースから取得できる値。デリゲートからデータベースにアクセスできますが、リクエストを送信するには、comboBoxの行が必要です。
だから私の質問は、テーブルのすべての行をどのように反復処理し、カスタムデリゲートの内部から何らかのアクションを実行できますか?
それはここに助けることができる場合は、私のカスタムデリゲートクラスです:
class ComboBoxDelegate(QtGui.QItemDelegate):
def __init__(self, parent, itemslist):
QtGui.QItemDelegate.__init__(self, parent)
self.itemslist = itemslist
self.parent = parent
def paint(self, painter, option, index):
# Get Item Data
value = index.data(QtCore.Qt.DisplayRole).toInt()[0]
# value = self.itemslist[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)
self.parent.openPersistentEditor(index)
def createEditor(self, parent, option, index):
##get the "check" value of the row
# for row in range(self.parent.model.rowCount(self.parent)):
# print row
self.editor = QtGui.QComboBox(parent)
self.editor.addItems(self.itemslist)
self.editor.setCurrentIndex(0)
self.editor.installEventFilter(self)
self.connect(self.editor, QtCore.SIGNAL("currentIndexChanged(int)"), self.editorChanged)
return self.editor
# def setEditorData(self, editor, index):
# value = index.data(QtCore.Qt.DisplayRole).toInt()[0]
# editor.setCurrentIndex(value)
def setEditorData(self, editor, index):
text = self.itemslist[index.data(QtCore.Qt.DisplayRole).toInt()[0]]
pos = self.editor.findText(text)
if pos == -1:
pos = 0
self.editor.setCurrentIndex(pos)
def setModelData(self,editor,model,index):
value = self.editor.currentIndex()
model.setData(index, QtCore.QVariant(value))
def updateEditorGeometry(self, editor, option, index):
self.editor.setGeometry(option.rect)
def editorChanged(self, index):
check = self.editor.itemText(index)
id_seq = self.parent.selectedIndexes[0][0]
update.updateCheckSeq(self.parent.db, id_seq, check)
そして、私はそれがこのようQTableViewをfromthe呼び出す:あなたの注意のために、私は十分に明確だった
self.setEditTriggers(QtGui.QAbstractItemView.CurrentChanged)
self.viewport().installEventFilter(self)
self.setItemDelegateForColumn(13,ComboBoxDelegate(self, self.checkValues))
ホープ、感謝
私は、正しい選択項目をcompboBoxesに表示することができました。しかし、あなたが述べたように私はデリゲートからデータベースにアクセスしていますが、あなたが提案したものを実装する方法は実際には分かりません。そして私はまだ問題があります。コンボボックスは、tableViewを並べ替えると「フォロー」しません。 – Johanna
モデルからデリゲートのペイントを呼び出すことは可能ですか? – Johanna
しかし、モデルはどのようにデリゲートについて知っていますか、大丈夫です。モデルへの代理参照を渡すことができます。デリゲートは、プレゼンテーションを改善して(つまり、ビューを改善して)モデルを作成し、相互依存関係を委譲することでコードが脆弱になります。 – sateesh