On this page

QRangeModel::RowOptions Struct

template <typename T> struct QRangeModel::RowOptions

The RowOptions template provides a customization point to control how QRangeModel represents types used as rows. More...

This struct was introduced in Qt 6.10.

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 &row)
(since 6.12) QVariant headerData(int section, int role)
(since 6.12) QMimeData *mimeData(const QModelIndex &range)
(since 6.12) QMimeData *mimeData(const auto &range)
(since 6.12) QStringList mimeTypes()
const QRangeModel::RowCategory rowCategory

Detailed Description

RowOptions<T> is a struct template where T specifies the row type. Specialize this template for the type used in your range, and add the relevant members.

template <>
struct QRangeModel::RowOptions<ColorEntry>
{
    ...
};

See also ItemAccess.

Member Function Documentation

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

[static, since 6.12] bool RowOptions::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.

These functions were introduced in Qt 6.12.

See also QAbstractItemModel::canDropMimeData().

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

[static, since 6.12] template <typename inserter:auto> auto RowOptions::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 rows 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::RowOptions<ColorEntry>
{
    static bool dropMimeData(const QMimeData *mimeData, auto inserter)
    {
        const QByteArray data = mimeData->data(mimeTypes().first());
        if (data.isEmpty())
            return false;
        QXmlStreamReader stream(data);
        while (!stream.atEnd()) {
            stream.readNext();
            if (stream.isStartElement() && stream.name() == u"li"_s)
                inserter = ColorEntry(stream.readElementText());
        }

        return true;
    }
};

The inserter behaves like a std::back_insert_iterator and can be used with e.g. std::copy. The above snippets omits error handling, which perhaps would require storing the decoded items in a separate list that can be discarded in case of an error. The following would efficiently move the decoded rows into the model:

static bool dropMimeData(const QMimeData *mimeData, auto inserter)
{
    QList<ColorEntry> decodedEntries;
    // read stream and populate decodedEntries

    if (stream.hasError())
        return false;
    std::copy(std::move_iterator(decodedRows.begin()), std::move_iterator(decodedRows.end()),
                inserter);
    return true;
}

Optionally, decoded rows can be paired with the relative row number it should have in the inserted range of rows.

int row = 0;
for (const auto &decodedRow : decodedRows) {
    inserter = {decodedRow, row};
    row += 2;
}

This implementation keeps an empty row between each decoded row.

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

These functions were introduced in Qt 6.12.

See also QAbstractItemModel::dropMimeData().

[static, since 6.12] Qt::ItemFlags RowOptions::flags(const T &row)

Implement this class member to return the flags for all items in row.

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

This will be overwritten by a customization of ItemAccess::flags.

If this member is not provided, then QRangeModel computes the flags based on the range it was constructed from.

This function was introduced in Qt 6.12.

See also QAbstractItemModel::flags().

[static, since 6.12] QVariant RowOptions::headerData(int section, int role)

Implement this class member to return the header data QRangeModel should return for the role of the section in the horizontal header.

template <>
struct QRangeModel::RowOptions<ColorEntry>
{
    static QVariant headerData(int section, int role)
    {
        switch (section) {
            // ...
        }
        return {};
    }
};

If this member is not provided, then QRangeModel returns type-specific default values from the headerData() implementation.

This function was introduced in Qt 6.12.

See also QAbstractItemModel::headerData().

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

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

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

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

template <>
struct QRangeModel::RowOptions<ColorEntry>
{
    template <typename Items>
    static QMimeData *mimeData(const Items &items)
    {
        if (items.isEmpty())
            return nullptr;
        QByteArray data;
        QXmlStreamWriter stream(&data);
        stream.writeStartElement("ul");
        for (const auto &[item, index] : items)
            stream.writeTextElement("li", item.colorName());
        stream.writeEndElement();

        QMimeData *mimeData = new QMimeData;
        mimeData->setData(mimeTypes().first(), data);
        return mimeData;
    }
};
for (const auto &[row, index] : range) {
    // ...
}

Fully selected rows will be included in the range only once, paired with an index that holds the row number and parent, and a column of -1. Partially selected rows are included in the range multiple times, once for each selected column.

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

These functions were introduced in Qt 6.12.

See also QAbstractItemModel::mimeData().

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

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

template <>
struct QRangeModel::RowOptions<ColorEntry>
{
    static QStringList mimeTypes()
    {
        return {
            u"text/html"_s
        };
    }
};

If this member is not provided, then QRangeModel returns the default mime type, "application/x-qabstractitemmodeldatalist", as supported by QAbstractItemModel. If the returned list does not include that mime type, then it will not be supported.

This function was introduced in Qt 6.12.

See also QAbstractItemModel::mimeTypes().

Member Variable Documentation

const QRangeModel::RowCategory RowOptions::rowCategory

Set this static compile-time constant to one of the values in the RowCategory enumerator to define how QRangeModel should interpret the elements of the range. Not providing this constant is the equivalent of RowCategory::Default.

class ColorEntry
{
    Q_GADGET
    Q_PROPERTY(QString display READ colorName)
    Q_PROPERTY(QColor decoration READ decoration)
    Q_PROPERTY(QString toolTip READ toolTip)
public:
    ...
};
template <>
struct QRangeModel::RowOptions<ColorEntry>
{
    static constexpr auto rowCategory = QRangeModel::RowCategory::MultiRoleItem;
};

If the rowCategory is set to MultiRoleItem, then none of the other members will have any effect.

See also ItemAccess.

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