2

カスタム デリゲートを使用して、QTableView にコンボ ボックスの列を表示しています。

デフォルトの選択の問題 (ここにリンクの説明を入力してください) に加えて、QTableView のデータを (列ごとに、またはフィルターを適用して) 並べ替えるときに問題が発生します。コンボボックスは、グリッドが表示されていないときの位置にとどまります。

デリゲートの再描画を強制する方法はありますか? ペイント メソッドのコードを (インデックスなしで) コピーする必要がありましたが、これによりプログラムがクラッシュするだけでした。

よくわからない場合はお知らせください。

これが私のカスタムデリゲートのコードです:

 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)


    def updateDelegate(self, indexRow, indewCol):
        # index = self.parent.model.createIndex(indexRow, indewCol)

        seq_id = self.parent.model.arraydata[indexRow][0]
        print seq_id
        check = select.getCheck(self.parent.db, seq_id)
        check = check[0][0]
        print check
        if check != '':
            pos = self.checkDict[check]
        else:
            pos = 0
        self.editor.setCurrentIndex(pos)

そして、私は QTableView クラスからそれを呼び出します:

 self.setEditTriggers(QtGui.QAbstractItemView.CurrentChanged)
    self.viewport().installEventFilter(self)
    self.delegate = ComboBoxDelegate(self, self.checkValues)
    self.setItemDelegateForColumn(13,self.delegate)

(モデル クラスから) 列を並べ替えるときに updateDelegate 関数を呼び出します。

  def sort(self, Ncol, order):
        self.emit(QtCore.SIGNAL("layoutAboutToBeChanged()"))
        self.arraydata = sorted(self.arraydata, key=operator.itemgetter(Ncol))
        i = 0
        for row in self.arraydata:
            self.parent.delegate.updateDelegate(i, 13)
            i += 1
        if order == QtCore.Qt.DescendingOrder:
            self.arraydata.reverse()
        self.emit(QtCore.SIGNAL("layoutChanged()"))
4

1 に答える 1

6

カスタム paint() メソッドで QItemDelegate.paint() メソッドを呼び出す必要がありました。それが誰かを助けることを願っています。

于 2011-05-12T17:21:31.233 に答える