On this page

QRangeModel::ItemAccess Struct

template <typename T> struct QRangeModel::ItemAccess

The ItemAccess template provides a customization point to control how QRangeModel accesses role data of individual items. More...

This struct was introduced in Qt 6.11.

Static Public Members

(since 6.12) bool canDropMimeData(const QMimeData *data)
(since 6.12) bool canDropMimeData(const QMimeData *data, int action, int row, int column, const QModelIndex &parent)
(since 6.12) auto dropMimeData(const QMimeData *data, auto inserter)
(since 6.12) auto dropMimeData(const QMimeData *data, int action, int row, int column, const QModelIndex &parent, auto inserter)
(since 6.12) Qt::ItemFlags flags(const T &item)
(since 6.12) QMimeData *mimeData(const QModelIndex &range)
(since 6.12) QMimeData *mimeData(const auto &range)
(since 6.12) QStringList mimeTypes()
QVariant readRole(const T &item, int role)
bool writeRole(T &item, const QVariant &value, int role)

Detailed Description

ItemAccess<T> is a struct template where T specifies the item type. Specialize this template for the type used in your data structure, and implement the relevant class member functions.

template <>
struct QRangeModel::ItemAccess<ColorEntry>
{
    static Qt::ItemFlags flags(const ColorEntry &entry)
    {
        return entry.flags();
    }
    static QVariant readRole(const ColorEntry &entry, int role)
    {
        switch (role) {
            // ...
        }
        return {};
    }
    static bool writeRole(ColorEntry &entry, const QVariant &value, int role)
    {
        bool ok = false;
        switch (role) {
            // ...
        }
        return ok;
    }
    ...
};

A specialization of this type will take precedence over any predefined behavior, and over a corresponding specialization of RowOptions.

Note: Do not specialize this template for types you do not own.

Member Function Documentation

[static, since 6.12] bool ItemAccess::canDropMimeData(const QMimeData *data)

[static, since 6.12] bool ItemAccess::canDropMimeData(const QMimeData *data, int action, int row, int column, const QModelIndex &parent)

Implement one of these class members to return whether data can be dropped into the model. If the simplified version is implemented, then QRangeModel will validate that action is one of the supported drop actions. In the full version the implementation can return different results based on the position of the drop as specified by row, column, and parent.

This member is optional and not required for drag'n'drop customization. The default behavior returns whether data holds one of the supported mime types.

Note: This specialization will only be considered when all items in the model are backed by items of type T. For heterogenous models where different columns provide different data types, specialize RowOptions instead.

These functions were introduced in Qt 6.12.

See also QAbstractItemModel::canDropMimeData().

[static, since 6.12] template <typename inserter:auto> auto ItemAccess::dropMimeData(const QMimeData *data, auto inserter)

[static, since 6.12] template <typename inserter:auto> auto ItemAccess::dropMimeData(const QMimeData *data, int action, int row, int column, const QModelIndex &parent, auto inserter)

Implement one of these class members to decode the relevant entries in data into a sequence of items of type T, and drop these rows into the model by assigning each to the provided inserter. Return whether the operation was successful.

template <>
struct QRangeModel::ItemAccess<ColorEntry>
{
    static bool dropMimeData(const QMimeData *mimeData, auto inserter)
    {
        QByteArray data = mimeData->data(mimeTypes().first());
        if (data.isEmpty())
            return false;
        QTextStream stream(&data, QIODevice::ReadOnly);
        while (!stream.atEnd()) {
            QString colorName;
            stream >> colorName;
            inserter = ColorEntry(colorName);
        }
        return true;
    }
};

Optionally, decoded items can be paired with a row and column value relative to the drop location. If the mime data includes positional information, then this makes it possible to maintain the "shape" of the items.

for (const auto &decodedItem: decodedItems) {
    inserter = {
        decodedItem,
        {
            relativeRow,
            relativeColumn
        }
    };
}

Implement the version with action, row, column, and parent parameters for full control over the operation.

Return one of the DropOperation values to provide a hint to QRangeModel for how the dropped rows should be applied to the model. Alternatively, simply return true (the equivalent of DropOperation::Automatic) or false (the equivalent of DropOperation::DontDrop).

Note: This specialization will only be considered when all items in the model are backed by items of type T. For heterogenous models where different columns provide different data types, specialize RowOptions instead.

These functions were introduced in Qt 6.12.

See also QAbstractItemModel::dropMimeData().

[static, since 6.12] Qt::ItemFlags ItemAccess::flags(const T &item)

Implement this class member to return the flags for item.

template <>
struct QRangeModel::ItemAccess<ColorEntry>
{
    static Qt::ItemFlags flags(const ColorEntry &entry)
    {
        return entry.flags();
    }
};

This function was introduced in Qt 6.12.

See also QAbstractItemModel::flags().

[static, since 6.12] QMimeData *ItemAccess::mimeData(const QModelIndex &range)

[static, since 6.12] template <typename range:auto> QMimeData *ItemAccess::mimeData(const auto &range)

Implement one of these class members to return the mime data for the items in the provided range.

If the generic version is provided, then the iterator over range is bidirectional, and dereferences to a pair with an item of type T and the corresponding QModelIndex.

template <>
struct QRangeModel::ItemAccess<ColorEntry>
{
    static QMimeData *mimeData(const auto &range)
    {
        QByteArray data;
        QTextStream stream(&data, QIODevice::WriteOnly);
        for (const auto &[item, index] : range)
            stream << item.colorName() << Qt::endl;

        QMimeData *result = new QMimeData;
        result->setData(mimeTypes().first(), data);
        // Qt handles encoding into the default mime type
        return result;
    }
};

The entries in range are sorted in logical order, from top-most to last row, and from left-most column to last column.

Note: This specialization will only be considered when all items in the model are backed by items of type T. For heterogenous models where different columns provide different data types, specialize RowOptions instead.

These functions were introduced in Qt 6.12.

See also QAbstractItemModel::mimeData().

[static, since 6.12] QStringList ItemAccess::mimeTypes()

Implement this class member to return the list of mime types that a model holding items of type T can use to represent the model data during drag'n'drop operations.

template <>
struct QRangeModel::ItemAccess<ColorEntry>
{
    static QStringList mimeTypes()
    {
        return {
            u"text/plain"_s,
            u"application/x-qabstractitemmodeldatalist"_s
        };
    }
};

If the list includes the default mime type that Qt uses for drag'n'drop data, then QRangeModel will take care of the encoding and decoding of data for that mime type automatically.

Note: This specialization will only be considered when all items in the model are backed by items of type T. For heterogenous models where different columns provide different data types, specialize RowOptions instead.

This function was introduced in Qt 6.12.

See also QAbstractItemModel::mimeTypes().

[static] QVariant ItemAccess::readRole(const T &item, int role)

Implement this class member to return the data in item for the requested role.

template <>
struct QRangeModel::ItemAccess<ColorEntry>
{
    static QVariant readRole(const ColorEntry &entry, int role)
    {
        switch (role) {
            // ...
        }
        return {};
    }
};

Types for which ItemAccess is specialized with a readRole implementation are implicitly interpreted as multi-role items.

See also QAbstractItemModel::data().

[static] bool ItemAccess::writeRole(T &item, const QVariant &value, int role)

Implement this class member to set the data of item for the requested role to the provided value, and return whether the change was successful.

template <>
struct QRangeModel::ItemAccess<ColorEntry>
{
    static bool writeRole(ColorEntry &entry, const QVariant &value, int role)
    {
        bool ok = false;
        switch (role) {
            // ...
        }
        return ok;
    }
};

See also QAbstractItemModel::setData().

© 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.