本页内容

QIdentityProxyModel Class

QIdentityProxyModel 类以未修改的形式代理其源模型。更多内容...

头文件: #include <QIdentityProxyModel>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承自: QAbstractProxyModel

公共函数

QIdentityProxyModel(QObject *parent = nullptr)
virtual ~QIdentityProxyModel()
(since 6.8) bool handleSourceDataChanges() const
(since 6.8) bool handleSourceLayoutChanges() const

重新实现的公共函数

virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
virtual bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override
virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
virtual QItemSelection mapSelectionFromSource(const QItemSelection &selection) const override
virtual QItemSelection mapSelectionToSource(const QItemSelection &selection) const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const override
virtual bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild) override
virtual bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override
virtual QModelIndex parent(const QModelIndex &child) const override
virtual bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override
virtual void setSourceModel(QAbstractItemModel *newSourceModel) override
virtual QModelIndex sibling(int row, int column, const QModelIndex &idx) const override

受保护函数

(since 6.8) void setHandleSourceDataChanges(bool b)
(since 6.8) void setHandleSourceLayoutChanges(bool b)

详细说明

QIdentityProxyModel 可用于精确转发源模型的结构,不进行排序、过滤或其他转换。其概念类似于单位矩阵,即 A.I = A.

由于它不进行排序或过滤,因此该类最适合作为代理模型,用于转换源模型的data()。 例如,可以创建一个代理模型来定义所使用的字体、背景颜色或工具提示等。这免除了在创建模型结构的同一类中实现所有数据处理的必要,还可以用于创建可重用的组件。

这还提供了一种在源模型由第三方提供且无法修改的情况下更改数据的方法。

class DateFormatProxyModel : public QIdentityProxyModel
{
  // ...

  void setDateFormatString(const QString &formatString)
  {
    m_formatString = formatString;
  }

  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
  {
    if (role != Qt::DisplayRole)
      return QIdentityProxyModel::data(index, role);

    const QModelIndex sourceIndex = mapToSource(index);
    const QDateTime dateTime = sourceModel()->data(sourceIndex, SourceClass::DateRole).toDateTime();
    return dateTime.toString(m_formatString);
  }

  QMap<int, QVariant> itemData(const QModelIndex &proxyIndex) const override
  {
      QMap<int, QVariant> map = QIdentityProxyModel::itemData(proxyIndex);
      map[Qt::DisplayRole] = data(proxyIndex);
      return map;
  }

private:
  QString m_formatString;
};

另请参阅 QAbstractProxyModel 、Model/View 编程以及QAbstractItemModel 。

成员函数文档

[explicit] QIdentityProxyModel::QIdentityProxyModel(QObject *parent = nullptr)

根据给定的parent 构建一个身份模型。

[virtual noexcept] QIdentityProxyModel::~QIdentityProxyModel()

销毁此标识符模型。

[override virtual] int QIdentityProxyModel::columnCount(const QModelIndex &parent = QModelIndex()) const

重写了:QAbstractItemModel::columnCount(const QModelIndex &parent) const。

[override virtual] bool QIdentityProxyModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)

重写了:QAbstractProxyModel::dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)。

[since 6.8] bool QIdentityProxyModel::handleSourceDataChanges() const

如果该代理模型处理源模型的数据变更,则返回true ;否则返回false 。

该函数在 Qt 6.8 中引入。

另请参阅 setHandleSourceDataChanges()。

[since 6.8] bool QIdentityProxyModel::handleSourceLayoutChanges() const

如果该代理模型处理源模型的布局变化,则返回true ;否则返回false 。

该函数在 Qt 6.8 中引入。

另请参阅 setHandleSourceLayoutChanges()。

[override virtual] QVariant QIdentityProxyModel::headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const

重写了:QAbstractProxyModel::headerData(int section, Qt::Orientation orientation, int role) const。

[override virtual] QModelIndex QIdentityProxyModel::index(int row, int column, const QModelIndex &parent = QModelIndex()) const

重写了:QAbstractItemModel::index(int row, int column, const QModelIndex &parent) const。

[override virtual] bool QIdentityProxyModel::insertColumns(int column, int count, const QModelIndex &parent = QModelIndex())

重写了:QAbstractItemModel::insertColumns(int column, int count, const QModelIndex &parent)。

[override virtual] bool QIdentityProxyModel::insertRows(int row, int count, const QModelIndex &parent = QModelIndex())

重写了:QAbstractItemModel::insertRows (int row, int count, const QModelIndex &parent)。

[override virtual] QModelIndex QIdentityProxyModel::mapFromSource(const QModelIndex &sourceIndex) const

重写了:QAbstractProxyModel::mapFromSource(const QModelIndex &sourceIndex) const。

[override virtual] QItemSelection QIdentityProxyModel::mapSelectionFromSource(const QItemSelection &selection) const

重写了:QAbstractProxyModel::mapSelectionFromSource(const QItemSelection &sourceSelection) const。

[override virtual] QItemSelection QIdentityProxyModel::mapSelectionToSource(const QItemSelection &selection) const

重写了:QAbstractProxyModel::mapSelectionToSource(const QItemSelection &proxySelection) const。

[override virtual] QModelIndex QIdentityProxyModel::mapToSource(const QModelIndex &proxyIndex) const

重写了:QAbstractProxyModel::mapToSource(const QModelIndex &proxyIndex) const。

[override virtual] QModelIndexList QIdentityProxyModel::match(const QModelIndex &start, int role, const QVariant &value, int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const

重写了以下函数:QAbstractItemModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const。

[override virtual] bool QIdentityProxyModel::moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild)

重写了:QAbstractItemModel::moveColumns (const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild)。

[override virtual] bool QIdentityProxyModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)

重写了:QAbstractItemModel::moveRows (const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)。

[override virtual] QModelIndex QIdentityProxyModel::parent(const QModelIndex &child) const

重写了:QAbstractItemModel::parent(const QModelIndex &index) const。

[override virtual] bool QIdentityProxyModel::removeColumns(int column, int count, const QModelIndex &parent = QModelIndex())

重写了:QAbstractItemModel::removeColumns (int column, int count, const QModelIndex &parent)。

[override virtual] bool QIdentityProxyModel::removeRows(int row, int count, const QModelIndex &parent = QModelIndex())

重写了:QAbstractItemModel::removeRows (int row, int count, const QModelIndex &parent)。

[override virtual] int QIdentityProxyModel::rowCount(const QModelIndex &parent = QModelIndex()) const

重写:QAbstractItemModel::rowCount(const QModelIndex &parent) const。

[protected, since 6.8] void QIdentityProxyModel::setHandleSourceDataChanges(bool b)

如果b 的值为true ,则该代理模型将处理源模型的数据变化(通过连接到QAbstractItemModel::dataChanged 信号)。

默认情况下,该代理模型会处理源模型的数据变更。

在QIdentityProxyModel 的子类中,如果您需要特别处理源模型的数据变化,将此属性设置为false 可能会很有用。

注意:调用 此方法仅在调用setSourceModel() 之后才会生效。

该函数自 Qt 6.8 起引入。

另请参阅 handleSourceDataChanges()。

[protected, since 6.8] void QIdentityProxyModel::setHandleSourceLayoutChanges(bool b)

如果b 的值为true ,则该代理模型将处理源模型的布局变化(通过连接到QAbstractItemModel::layoutAboutToBeChanged 和QAbstractItemModel::layoutChanged 信号)。

默认情况下,该代理模型会处理源模型的布局更改。

在QIdentityProxyModel 的子类中,如果您需要特别处理源模型的布局变化,将此属性设置为false 可能会很有用。

注意:调用 此方法仅在调用setSourceModel() 之后才会生效。

此函数在 Qt 6.8 中引入。

另请参阅 handleSourceLayoutChanges()。

[override virtual] void QIdentityProxyModel::setSourceModel(QAbstractItemModel *newSourceModel)

重写了:QAbstractProxyModel::setSourceModel(QAbstractItemModel *sourceModel)。

[override virtual] QModelIndex QIdentityProxyModel::sibling(int row, int column, const QModelIndex &idx) const

重写了:QAbstractProxyModel::sibling(int row, int column, const QModelIndex &idx) const。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.