使用QSortFilterProxyModel进行工作QSqlRelationalDelegate

时间:2015-02-03 16:22:38

标签: qt pyqt qsqltablemodel qsortfilterproxymodel qstyleditemdelegate

我一直在使用QSortFilterProxyModels。但是,如果在源模型上设置了QSqlRelation以及视图上的QSqlRelationalDelegate,则只要将视图切换到代理模型,QSqlRelationalDelegate就会消失,保留基本{ {1}}或QLineEdit

如何让视图中的列同时使用QSpinBoxQSortFilterProxyModel,并提供预期的QSqlRelationalDelegate下拉列表?

2 个答案:

答案 0 :(得分:1)

这是一种更好的方法:QSqlRelationalTableModel with QSqlRelationalDelegate not working behind QAbstractProxyModel

需要使用mapToSource,因为视图索引可以与模型索引不同。

class Delegate(QtSql.QSqlRelationalDelegate):
    """
    Delegate handles custom editing. This allows the user to have good
    editing experience.

    Because the join table uses a proxy model a subclass QSqlRelationalDelegate
    is required. This is to support the foreign key combobox.
    """


    def __init__(self, parent = None):
        """
        Class constructor.
        """
        # Python super lets you avoid referring to the base class explicitly.
        super(Delegate, self).__init__(parent)

    def createEditor(self, parent, option, index):
        """
        This creates the editors in the delegate.
        Reimplemented from QAbstractItemDelegate::createEditor().
        Returns the widget used to edit the item specified by
        index for editing.

        The parent widget and style option are used to control how the
        editor widget appears.

        1. Get the model associated with the view. In this case it is the
            QSortFilterProxyModel.

        2. Because with a proxy model the views index does not have to be the
        same as the models index. If one sorts,
        then the index are not the same.

        3. mapToSource.
            This is why mapToSource is being used.
            mapToSouce Reimplement this function to return the
            model index in the proxy model that corresponds to the
            sourceIndex from the source model.

        4. Return the createEditor with the base index being set to the source
            model and not the proxy model.
        """
        if index.column() == 2:
            proxy = index.model()
            base_index = proxy.mapToSource(index)
            return super(Delegate, self).createEditor(parent, option, base_index)
        else:
            return super(Delegate, self).createEditor(parent, option, index)


    def setEditorData(self, editor, index):
        """
        Once the editor has been created and given to the view
        the view calls setEditorData().
        This gives the delegate the opportunity to populate the editor
        with the current data, ready for the user to edit.

        Sets the contents of the given editor to the data for the item
        at the given index.

        Note that the index contains information about the model being used.

        The base implementation does nothing.
        If you want custom editing you will need to reimplement this function.

        1. Get the model which is a QSortFilterProxyModel.

        2. Call mapToSource().
        Because with a proxy model the views index does not have to be the
        same as the models index. If one sorts,
        then the index are not the same.
        This is why mapToSource is being used. MapToSouce Reimplement this
        function to return the model index in the proxy model
        that corresponds to the sourceIndex from the source model.

        3. Return setEditorData with the editor and the mapToSource index.

        4. Else for all other columns return the base method.
        """

        if index.column() == 2:
            proxy = index.model()
            base_index = proxy.mapToSource(index)
            return super(JoinDelegate, self).setEditorData(editor, base_index)
        else:
              return super(Delegate, self).setEditorData(editor, index)


    def setModelData(self, editor, model, index):
        if index.column() == 2:
            base_model = model.sourceModel()
            base_index = model.mapToSource(index)
            return super(JoinDelegate, self).setModelData(editor, base_model, base_index)
        else:
             super(Delegate, self).setModelData(editor, model, index)


    def sizeHint(self, option, index):
        """
        This pure abstract function must be reimplemented if you want to
        provide custom rendering. The options are specified by option and
        the model item by index.

        """
        if index.isValid():
            column = index.column()
            text = index.model().data(index)
            document = QtGui.QTextDocument()
            document.setDefaultFont(option.font)
            # change cell Width, height (One can add or subtract to change the relative dimension)
            return QtCore.QSize(QtSql.QSqlRelationalDelegate.sizeHint(self, option, index).width() - 200,
                QtSql.QSqlRelationalDelegate.sizeHint(self, option, index).height() + 40)
        else:
            return super(Delegate, self).sizeHint(option, index)

答案 1 :(得分:0)

默认情况下,QSqlRelationalDelegate无法处理代理模型,因此您必须对其进行子类化。下面的内容可能远非完美,所以欢迎评论/调整,但是对于混合了QSqlRelations /直接数据的视图一直运行良好,没有毛刺。

class ProxyDelegate(QSqlRelationalDelegate):
    def __init__(self):
        QSqlRelationalDelegate.__init__(self)

    def createEditor(self, p, o, i):                                                # parent, option, index
        if i.model().sourceModel().relation(i.column()).isValid():                  # if the column has a QSqlRelation, then make the expected QComboBox
            e = QComboBox(p)
            return e      
        else:
            return QStyledItemDelegate(p).createEditor(p, o, i)

    def setEditorData(self, e, i):
        m = i.model()
        sM = m.sourceModel()
        relation = sM.relation(i.column()) 
        if relation.isValid():                                                              
            m = i.model()
            sM = m.sourceModel()
            relation = sM.relation(i.column())
            pModel = QSqlTableModel()                                                # pModel means populate model.  Because I've aimed for generic use, it makes a new QSqlTableModel, even if one already exists elsewhere for that SQL table
            pModel.setTable(relation.tableName())
            pModel.select()
            e.setModel(pModel)
            pModel.sort(pModel.fieldIndex(relation.displayColumn()), Qt.AscendingOrder)  # default sorting.  A custom attribute would need adding to each source model class, in order for this line to know the desired sorting order for this QComboBox delegate   
            e.setModelColumn(pModel.fieldIndex(relation.displayColumn()))            
            e.setCurrentIndex(e.findText(m.data(i).toString()))       
        else:
            return QStyledItemDelegate().setEditorData(e, i)

    def setModelData(self, e, m, i):
        m = i.model()                                                                # this could probably be written more elegantly so you don't need to create another SqlModel
        sM = m.sourceModel()
        relation = sM.relation(i.column())
        table = relation.tableName()
        indexColumn = relation.indexColumn()
        indexColumnId = sM.fieldIndex(indexColumn)
        displayColumn = relation.displayColumn() 
        if relation.isValid():            
            pModel = QSqlTableModel()
            pModel.setTable(relation.tableName())
            pModel.select()
            displayColumnId = pModel.fieldIndex(displayColumn)
            chosenRowInPModel = pModel.match(pModel.index(0, displayColumnId), Qt.DisplayRole, e.currentText())[0].row()            
            chosenIdInPModel = pModel.data(pModel.index(chosenRowInPModel, indexColumnId)).toString()
            m.setData(i, chosenIdInPModel)
            self.closeEditor.emit(e, QAbstractItemDelegate.NoHint)
        else:
            QStyledItemDelegate().setModelData(e, m, i)