QRangeModelAdapter Class | Qt Core | Qt 6.11.0
Description: QRangeModelAdapter provides QAbstractItemModel-compliant access to any C++ range.

- Qt 6.11
- Qt Core
- C++ Classes
- QRangeModelAdapter
- Qt 6.11.0 Reference Documentation

# QRangeModelAdapter Class

QRangeModelAdapter provides QAbstractItemModel-compliant access to any C++ range. More...

Header: | `#include <QRangeModelAdapter>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`
Since: | Qt 6.11
Status: | Preliminary

This class is under development and is subject to change.

- List of all members, including inherited members

This class is equality-comparable.

This class is equality-comparable with Range.

## Public Functions

| QRangeModelAdapter(Range &&range)
| QRangeModelAdapter(Range &&range, Protocol &&protocol)
void | assign(NewRange &&newRange)
void | assign(std::initializer_list<Row> newRange)
void | assign(InputIterator first, Sentinel last)
auto | at(QSpan<const int> path)
auto | at(int row)
auto | at(int row)
auto | at(int row)
decltype(auto) | at(QSpan<const int> path) const
auto | at(int row) const
decltype(auto) | at(int row) const
auto | at(QSpan<const int> path, int column)
auto | at(int row, int column)
auto | at(QSpan<const int> path, int column) const
auto | at(int row, int column) const
int | columnCount() const
QVariant | data(int row) const
QVariant | data(QSpan<const int> path, int column) const
QVariant | data(int row, int role) const
QVariant | data(int row, int column) const
QVariant | data(QSpan<const int> path, int column, int role) const
QVariant | data(int row, int column, int role) const
bool | hasChildren(QSpan<const int> row) const
bool | hasChildren(int row) const
QModelIndex | index(int row) const
QModelIndex | index(QSpan<const int> path, int column) const
QModelIndex | index(int row, int column) const
bool | insertColumn(int before)
bool | insertColumn(int before, D &&data)
bool | insertColumns(int before, C &&data)
bool | insertRow(QSpan<const int> before)
bool | insertRow(int before)
bool | insertRow(QSpan<const int> before, D &&data)
bool | insertRow(int before, D &&data)
bool | insertRows(QSpan<const int> before, C &&data)
bool | insertRows(int before, C &&data)
Model * | model() const
bool | moveColumn(int from, int to)
bool | moveColumns(int from, int count, int to)
bool | moveRow(QSpan<const int> source, QSpan<const int> destination)
bool | moveRow(int source, int destination)
bool | moveRows(QSpan<const int> source, int count, QSpan<const int> destination)
bool | moveRows(int source, int count, int destination)
const QRangeModelAdapter<Range, Protocol, Model>::range_type & | range() const
bool | removeColumn(int column)
bool | removeColumns(int column, int count)
bool | removeRow(QSpan<const int> path)
bool | removeRow(int row)
bool | removeRows(QSpan<const int> path, int count)
bool | removeRows(int row, int count)
int | rowCount() const
int | rowCount(QSpan<const int> row) const
int | rowCount(int row) const
bool | setData(int row, const QVariant &value, int role = Qt::EditRole)
bool | setData(QSpan<const int> path, int column, const QVariant &value, int role = Qt::EditRole)
bool | setData(int row, int column, const QVariant &value, int role = Qt::EditRole)
QRangeModelAdapter<Range, Protocol, Model> & | operator=(NewRange &&newRange)
QRangeModelAdapter<Range, Protocol, Model> & | operator=(std::initializer_list<Row> newRange)
auto | operator[](QSpan<const int> path)
auto | operator[](int row)
auto | operator[](int row)
auto | operator[](int row)
decltype(auto) | operator[](QSpan<const int> path) const
auto | operator[](int row) const
decltype(auto) | operator[](int row) const

## Private Types

| range_type

## Related Non-Members

bool | operator!=(const QRangeModelAdapter<Range, Protocol, Model> &lhs, const QRangeModelAdapter<Range, Protocol, Model> &rhs)
bool | operator==(const QRangeModelAdapter<Range, Protocol, Model> &lhs, const QRangeModelAdapter<Range, Protocol, Model> &rhs)

## Detailed Description

QRangeModelAdapter provides a type-safe and structure-aware C++ API around a C++ range and a QRangeModel. Modifications made to the C++ range using the adapter will inform clients of the QRangeModel about the changes. This makes sure that item views are updated, caches are cleaned, and persistent item indexes are invalidated and adapted correctly.

### Construction and model ownership

QRangeModelAdapter has to be constructed from a C++ range. As with QRangeModel, the range can be provided by lvalue or rvalue reference, as a reference wrapper, and as a raw or smart pointer.

     std::vector<int> data = {1, 2, 3, 4, 5};
     QRangeModelAdapter adapter(&data);

The exact type that gets constructed is deducted by the compiler based on the constructor argument(s), and the differences between value categories can result in different behavior. Therefore, you should not explicitly specify the template parameters. C++ however does not allow class template argument deduction for non-static class data members. To make QRangeModelAdapter a data member of a class, use the following pattern:

     class Backend
     {
         using Adapter = decltype(QRangeModelAdapter(std::vector<Book>{}));
         Adapter adapter = Adapter(std::vector<Book>{});

This creates a QRangeModelAdapter operating on an unnamed (i.e. rvalue reference) vector of Books. To make the adapter operate on a reference wrapper to a list of Books, use this instead:

         QList<Book> books = { /* ... */ };
         using Adapter = decltype(QRangeModelAdapter(std::ref(books)));
         Adapter adapter = Adapter(std::ref(books));
     };

Constructing the adapter from a range implicitly constructs a QRangeModel instance from that same range. Use model() to get it, and pass it to Qt Widgets or Qt Quick item views as usual.

     QListView listView;
     listView.setModel(adapter.model());

The adapter owns the model. QRangeModelAdapter is a value type, so it can be copied and moved. All copies share the same QRangeModel, which will be destroyed when the last copy of the adapter is destroyed.

If the adapter was created from an lvalue or rvalue reference, then the adapter and model will operate on a copy of the original range object. Otherwise, modifications made through the adapter or model will be written to the original range object. To access the updated range, use range():

     QList<Book> books = {
         // ...
     };
     QRangeModelAdapter adapter(books);
     tableView.setModel(adapter.model());

     // show UI and where the user can modify the list

     QList<Book> modifiedBooks = adapter.range();

To replace the entire range data with data from another (compatible) range, use the assign() function, or the assignment operator.

     // reset to the original
     adapter = books;
     // or
     adapter.assign(books);

### Accessing item data

The QRangeModelAdapter API provides type-safe read and write access to the range that the model operates on. The adapter API is based on the typical API for C++ containers and ranges, including iterators. To access individual rows and items, use at(), the corresponding subscript `operator[]`, or data(). Which overloads of those functions are available depends on the range for which the adapter was constructed.

#### Reading item data as a QVariant

The data() function always returns a QVariant with the value stored at the specified position and role. In a list, an item can be accessed by a single integer value specifying the row:

     QVariant listItem = listAdapter.data(row);

If the range is a table, then items are specified by row and column:

     QVariant tableItem = tableAdapter.data(row, column);

If the range is a tree, then items are located using a path of rows, and a single column value:

     QVariant treeItem = treeAdapter.data({path, to, branch}, column);

Using a single integer as the row provides access to the toplevel tree items.

     QRangeModelAdapter listOfBooks(QList<Book>{
         // ~~~
     });
     QString bookTitle = listOfBooks.data(0, Book::TitleRole).toString();
     Book multiRoleItem = listOfBooks.data(0).value<Book>();

If no role is specified, then the QVariant will hold the entire item at the position. Use the QVariant::fromValue() template function to retrieve a copy of the item.

#### Reading and writing using at()

That the data() function returns a QVariant makes it flexible, but removes type safety. For ranges where all items are of the same type, the at() function provides a type-safe alternative that is more compatible with regular C++ containers. As with data(), at() overloads exist to access an item at a row for lists, at a row/column pair for table, and a path/column pair for trees. However, at() always returns the whole item at the specified position; it's not possible to read an individual role values for an item.

As expected from a C++ container API, the const overloads of at() (and the corresponding subscript `operator[]`) provide immutable access to the value, while the mutable overloads return a reference object that a new value can be assigned to. Note that a QRangeModelAdapter operating on a const range behaves in that respect like a const QRangeModelAdapter. The mutable overloads are removed from the overload set, so the compiler will always select the const version. Trying to call a function that modifies a range will result in a compiler error, even if the adapter itself is mutable:

     const QStringList strings = {"On", "Off"};
     QRangeModelAdapter adapter(strings);
     adapter.at(0) = "Undecided"; // compile error: return value of 'at' is const
     adapter.insertRow(0); // compiler error: requirements not satisfied

The returned reference objects are wrappers that convert implicitly to the underlying type, have a `get()` function to explicitly access the underlying value, and an `operator->()` that provides direct access to const member functions. However, to prevent accidental data changes that would bypass the QAbstractItemModel notification protocol, those reference objects prevent direct modifications of the items.

Note: Accessing the reference object always makes a call to the model to get a copy of the value. This can be expensive; for performance critical access to data, store a copy.

##### Item access

If the range is represented as a list, then only the overloads taking a row are available.

     QRangeModelAdapter list(std::vector<int>{1, 2, 3, 4, 5});
     listView.setModel(list.model());

     int firstValue = list.at(0); // == 1
     list.at(0) = -1;
     list.at(1) = list.at(4);

The const overload returns the item at that row, while the mutable overload returns a wrapper that implicitly converts to and from the value type of the list.

     QRangeModelAdapter books(QList<Book>{
         // ~~~
     });
     Book firstBook = books.at(0);
     Book newBook = {};
     books.at(0) = newBook; // dataChanged() emitted

Assign a value to the wrapper to modify the data in the list. The model will emit dataChanged() for all roles.

When using the mutable overloads, you can also access the item type's const members using the overloaded arrow operator.

     QString title = books.at(0)->title();

It is not possible to access non-const members of the item. Such modifications would bypass the adapter, which couldn't notify the model about the changes. To modify the value stored in the model, make a copy, modify the properties of the copy, and then write that copy back.

     // books.at(0)->setRating(5); - not possible even though 'books' is not const
     firstBook = books.at(0);
     firstBook.setRating(5);
     books.at(0) = firstBook; // dataChanged() emitted

This will make the model emit dataChanged() for this item, and for all roles.

If the range is represented as a table, then you can access an individual item by row and columns, using the at(row, column) overload. For trees, that overload gives access to top-level items, while the at(path, column) overload provides access to items nested within the tree.

Accessing an individual item in a table or tree is equivalent to accessing an item in a list.

     QRangeModelAdapter table(std::vector<std::vector<double>>{
         {1.0, 2.0, 3.0, 4.0, 5.0},
         {6.0, 7.0, 8.0, 9.0, 10.0},
     });
     tableView.setModel(table.model());

     double value = table.at(0, 2); // value == 3.0
     table.at(0, 2) = value * 2; // table[0, 2] == 6.0

If the range doesn't store all columns using the same data type, then `at(row,column)` returns a (a reference wrapper with a) QVariant holding the item.

     QRangeModelAdapter table(std::vector<std::tuple<int, QString>>{
         // ~~~
     });
     int number = table.at(0, 0)->toInt();
     QString text = table.at(0, 1)->toString();

#### Accessing rows in tables and trees

For tables and trees, the overloads of at() and subscript `operator[]` without the column parameter provide access to the entire row. The value returned by the const overloads will be a reference type that gives access to the row data. If that row holds pointers, then that reference type will be a view of the row, giving access to pointers to const items.

##### Table row access

The at(int) overload is still available, but it returns the entire table wor. This makes it possible to work with all the values in the row at once.

     const auto &constTable = table;
     const std::vector<double> &topRow = constTable.at(0);

As with items, the const overload provides direct access to the row type, while the mutable overload returns a wrapper that acts as a reference to the row. The wrapper provides access to const member functions using the overloaded arrow operator. To modify the values, write a modified row type back.

     auto lastRow = table.at(table.rowCount() - 1);
     lastRow = { 6.5, 7.5, 8.0, 9.0, 10 }; // emits dataChanged() for entire row

When assigning a new value to the row, then the model emits the dataChanged() signal for all items in the row, and for all roles.

Note: When using a row type with runtime sizes, such as a `std::vector` or a QList, make sure that the new row has the correct size.

##### Tree row access

Rows in trees are specified by a sequence of integers, one entry for each level in the tree. Note that in the following snippets, the Tree is a range holding raw row pointers, and that the adapter is created with an rvalue reference of that range. This gives the QRangeModel ownership over the row data.

     QRangeModelAdapter tree = QRangeModelAdapter(Tree{
         new TreeRow{"Germany", 357002, Tree{
                 new TreeRow("Bavaria", 70550)
             },
         },
         new TreeRow{"France", 632702},
     });
     treeView.setModel(tree.model());

     auto germanyData = tree.at(0);
     auto bavariaData = tree.at({0, 0});

The overload of at() taking a single row value provides access to the top-level rows, or items in the top-level rows.

     auto germanyName = tree.at(0, 0);
     auto bavariaSize = tree.at({0, 0}, 1);

The basic pattern for accessing rows and items in a tree is identical to accessing rows and items in a table. However, the adapter will make sure that the tree structure is maintained when modifying entire rows.

     // deletes the old row - tree was moved in
     tree.at({0, 0}) = new TreeRow{"Berlin", 892};

In this example, a new row object is created, and assigned to the first child of the first top-level item.

### Iterator API

Use begin() and end() to get iterators over the rows of the model, or use ranged-for. If the range is a list of items, dereferencing the iterator will give access to item data.

     for (const auto &book : std::as_const(books)) {
         qDebug() << "The book" << book.title()
                  << "written by" << book.author()
                  << "has" << book.rating() << "stars";
     }

As with the const and mutable overloads of at(), a mutable iterator will dereference to a wrapper.

     for (auto book : books) {
         qDebug() << "The book" << book->title()
                  << "written by" << book->author()
                  << "has" << book->rating() << "stars";

         Book copy = book;
         copy.setRating(copy.rating() + 1);
         book = copy;
     }

Use the overloaded arrow operator to access const members of the item type, and assign to the wrapper to replace the value.

It the range is a table or tree, then iterating over the model will give access to the rows.

     QRangeModelAdapter table(std::vector<std::pair<int, QString>>{
         // ~~~
     });

     for (const auto &row : std::as_const(table)) {
         qDebug() << "Number is" << row->first << "and string is" << row->second;
     }

Both the const and the mutable iterator will dereference to a wrapper type for the row. This make sure that we can consistently iterate over each column, even if the underlying row type is not a range (e.g. it might be a tuple or gadget).

     for (const auto &row : std::as_const(table)) {
         for (const auto &item : row) {
             qDebug() << item; // item is a QVariant
         }
     }

When iterating over a mutable table we can overwrite the entire row.

     for (auto row : table) {
         qDebug() << "Number is" << row->first << "and string is" << row->second;
         row = std::make_pair(42, u"forty-two"_s);
     }

The model emits the dataChanged() signal for all items in all row, and for all roles.

     for (auto row : table) {
         for (auto item : row) {
             item = 42;
         }
     }

Iterating over the mutable rows allows us to modify individual items.

When iterating over a tree, the row wrapper has two additional member functions, hasChildren() and children(), that allow us to traverse the entire tree using iterators.

     for (auto row : tree) {
         if (row.hasChildren()) {
             for (auto child : row.children()) {
                 // ~~~
             }
         }
     }

The object returned by children() is a QRangeModelAdapter operating on the same model as the callee, but all operations will use the source row index as the parent index.

See also QRangeModel.

## Member Type Documentation

### `[alias]` QRangeModelAdapter::range_type

## Member Function Documentation

### `[default]` QRangeModelAdapter::QRangeModelAdapter(Range &&range, Protocol &&protocol)

### `[default]` QRangeModelAdapter::QRangeModelAdapter(Range &&range)

Constructs a QRangeModelAdapter that operates on range. For tree ranges, the optional protocol will be used for tree traversal.

See the QRangeModel constructor documentation for details on the requirements to Range, and how the value category of range changes the interaction between adapter, model, and range.

See also QRangeModel.

### template <typename NewRange = QRangeModelAdapter<Range, Protocol, Model>::range_type, QRangeModelAdapter<Range, Protocol, Model>::if_assignable_range<NewRange> = true> void QRangeModelAdapter::assign(NewRange &&newRange)

### template < typename NewRange = QRangeModelAdapter<Range, Protocol, Model>::range_type, QRangeModelAdapter<Range, Protocol, Model>::if_assignable_range<NewRange> = true, QRangeModelAdapter<Range, Protocol, Model>::unless_adapter<NewRange> = true > QRangeModelAdapter<Range, Protocol, Model> &QRangeModelAdapter::operator=(NewRange &&newRange)

Replaces the contents of the model with the rows in newRange, possibly using move semantics.

This function makes the model() emit the modelAboutToBeReset() and modelReset() signals.

#### Constraints

Participates in overload resolution only if `Range` is mutable, and newRange is of a type that can be assigned to `Range`, but not a QRangeModelAdapter.

See also range(), at(), model(), and assign().

### template <typename Row, QRangeModelAdapter<Range, Protocol, Model>::if_assignable_range<std::initializer_list<Row>> = true> void QRangeModelAdapter::assign(std::initializer_list<Row> newRange)

### template <typename Row, QRangeModelAdapter<Range, Protocol, Model>::if_assignable_range<std::initializer_list<Row>> = true> QRangeModelAdapter<Range, Protocol, Model> &QRangeModelAdapter::operator=(std::initializer_list<Row> newRange)

Replaces the contents of the model with the rows in newRange.

#### Constraints

Participates in overload resolution only if `Range` is mutable, and newRange can be assigned to `Range`.

See also range(), at, and model().

### template < typename InputIterator, typename Sentinel, typename I, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > void QRangeModelAdapter::assign(InputIterator first, Sentinel last)

Replaces the contents of the models with the rows in the range [first, last).

See also range(), at, and model().

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > auto QRangeModelAdapter::at(QSpan<const int> path)

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > auto QRangeModelAdapter::operator[](QSpan<const int> path)

Returns a mutable wrapper holding a reference to the tree row specified by path.

To modify the tree row, assign a new value to it. Assigning a new tree row will set the parent the new tree row to be the parent of the old tree row. However, neither the old nor the new tree row must have any child rows. To access the tree row, dereferencing the wrapper using `operator*()`, or use `operator->()` to access tree row members.

Note: Modifications to the range will invalidate the wrapper.

#### Constraints

Participates in overload resolution only if `Range` is a tree.

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_list<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > auto QRangeModelAdapter::at(int row)

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_list<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > auto QRangeModelAdapter::operator[](int row)

Returns the value at row wrapped into a mutable reference to the type stored in `Range`.

Note: Modifications to the range will invalidate that reference. To modify the reference, assign a new value to it. Unless the value stored in the `Range` is a pointer, it is not possible to access individual members of the stored value.

#### Constraints

Participates in overload resolution only if `Range` is a mutable list.

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_table<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > auto QRangeModelAdapter::at(int row)

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_table<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > auto QRangeModelAdapter::operator[](int row)

Returns a mutable reference to the row at row, as stored in `Range`.

#### Constraints

Participates in overload resolution only if `Range` is a mutable table, but not a tree.

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > auto QRangeModelAdapter::at(int row)

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > auto QRangeModelAdapter::operator[](int row)

Returns a mutable wrapper holding a reference to the tree row specified by row.

To modify the tree row, assign a new value to it. Assigning a new tree row will set the parent the new tree row to be the parent of the old tree row. However, neither the old nor the new tree row must have any child rows. To access the tree row, dereferencing the wrapper using `operator*()`, or use `operator->()` to access tree row members.

Note: Modifications to the range will invalidate the wrapper.

#### Constraints

Participates in overload resolution only if `Range` is a tree.

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true> decltype(auto) QRangeModelAdapter::at(QSpan<const int> path) const

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true> decltype(auto) QRangeModelAdapter::operator[](QSpan<const int> path) const

Returns a constant reference to the row specified by path, as stored in `Range`.

#### Constraints

Participates in overload resolution only if `Range` is a tree.

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_list<I> = true> auto QRangeModelAdapter::at(int row) const

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_list<I> = true> auto QRangeModelAdapter::operator[](int row) const

Returns the value at row as the type stored in `Range`.

#### Constraints

Participates in overload resolution only if `Range` is a list.

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::unless_list<I> = true> decltype(auto) QRangeModelAdapter::at(int row) const

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::unless_list<I> = true> decltype(auto) QRangeModelAdapter::operator[](int row) const

Returns a constant reference to the row at row, as stored in `Range`.

#### Constraints

Participates in overload resolution only if `Range` is a table or tree.

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > auto QRangeModelAdapter::at(QSpan<const int> path, int column)

Returns a mutable reference to the value stored as the item specified by path and column. If the item is a multi-role item, then this will be a reference to the entire item. If the rows in the `Range` store different types at different columns, then the return type will be a QVariant.

Note: Modifications to the range will invalidate that reference. To modify the reference, assign a new value to it. Unless the value stored in the `Range` is a pointer, it is not possible to access individual members of the stored value.

#### Constraints

Participates in overload resolution only if `Range` is a mutable tree.

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::unless_list<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > auto QRangeModelAdapter::at(int row, int column)

Returns a mutable reference to the value stored as the item specified by row and column. If the item is a multi-role item, then this will be a reference to the entire item.

Note: Modifications to the range will invalidate that reference. To modify the reference, assign a new value to it. Unless the value stored in the `Range` is a pointer, it is not possible to access individual members of the stored value.

#### Constraints

Participates in overload resolution only if `Range` is a mutable table.

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true> auto QRangeModelAdapter::at(QSpan<const int> path, int column) const

Returns a copy of the value stored as the item specified by path and column. If the item is a multi-role item, then this returns a copy of the entire item. If the rows in the `Range` store different types at different columns, then the return type will be a QVariant.

#### Constraints

Participates in overload resolution only if `Range` is a tree.

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::unless_list<I> = true> auto QRangeModelAdapter::at(int row, int column) const

Returns a copy of the value stored as the item specified by row and column. If the item is a multi-role item, then this returns a copy of the entire item. If the rows in the `Range` store different types at different columns, then the return type will be a QVariant.

#### Constraints

Participates in overload resolution only if `Range` is a table or tree.

### int QRangeModelAdapter::columnCount() const

Returns the number of columns. This will be one if the `Range` represents a list, otherwise this returns be the number of elements in each row.

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_list<I> = true> QVariant QRangeModelAdapter::data(int row) const

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_list<I> = true> QVariant QRangeModelAdapter::data(int row, int role) const

Returns a QVariant holding the data stored under the given role for the item at row, or an invalid QVariant if there is no item. If role is not specified, then returns a QVariant holding the complete item.

#### Constraints

Participates in overload resolution only if `Range` is a list.

See also setData() and at().

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true> QVariant QRangeModelAdapter::data(QSpan<const int> path, int column) const

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true> QVariant QRangeModelAdapter::data(QSpan<const int> path, int column, int role) const

Returns a QVariant holding the data stored under the given role for the item referred to by path and column, or an invalid QVariant if there is no data stored for that position or role. If role is not specified, then returns a QVariant holding the complete item.

#### Constraints

Participates in overload resolution only if `Range` is a tree.

See also setData() and at().

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::unless_list<I> = true> QVariant QRangeModelAdapter::data(int row, int column) const

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::unless_list<I> = true> QVariant QRangeModelAdapter::data(int row, int column, int role) const

Returns a QVariant holding the data stored under the given role for the item referred to by a row and column, or an invalid QVariant if there is no data stored for that position or role. If role is not specified, then returns a QVariant holding the complete item.

#### Constraints

Participates in overload resolution only if `Range` is a table or tree.

See also setData() and at().

### `[constexpr]` template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true> bool QRangeModelAdapter::hasChildren(int row) const

### `[constexpr]` template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true> bool QRangeModelAdapter::hasChildren(QSpan<const int> row) const

Returns whether there are any rows under row.

#### Constraints

Participates in overload resolution only if `Range` is a tree.

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_list<I> = true> QModelIndex QRangeModelAdapter::index(int row) const

Returns the QModelIndex for row.

#### Constraints

Participates in overload resolution only if `Range` is a one-dimensional list.

This is an overloaded function.

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true> QModelIndex QRangeModelAdapter::index(QSpan<const int> path, int column) const

Returns the QModelIndex for the item at column for the row in the tree specified by path.

#### Constraints

Participates in overload resolution only if `Range` is a tree.

This is an overloaded function.

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::unless_list<I> = true> QModelIndex QRangeModelAdapter::index(int row, int column) const

Returns the QModelIndex for the item at row, column.

#### Constraints

Participates in overload resolution only if `Range` is a table or tree.

This is an overloaded function.

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canInsertColumns<I> = true> bool QRangeModelAdapter::insertColumn(int before)

Inserts a single empty column before the column specified by before into all rows, and returns whether the insertion was successful. If before is the same value as columnCount(), then the column will be appended to each row.

#### Constraints

Participates in overload resolution only if `Range` has rows that support insertion of elements.

This is an overloaded function.

See also removeColumn(), insertColumns(), and insertRow().

### template < typename D, typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canInsertColumns<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_compatible_column_data<D> = true > bool QRangeModelAdapter::insertColumn(int before, D &&data)

Inserts a single column constructed from data before the column specified by before into all rows, and returns whether the insertion was successful. If before is the same value as columnCount(), then the column will be appended to each row.

If data is a single value, then the new entry in all rows will be constructed from that single value.

If data is a container, then the elements in that container will be used sequentially to construct the column for each subsequent row. If there are fewer elements in data than there are rows, then function wraps around and starts again from the first element.

#### Constraints

Participates in overload resolution only if `Range` has rows that support insertion of elements, and the elements can be constructed from the entries in data.

This is an overloaded function.

See also removeColumn(), insertColumns(), and insertRow().

### template < typename C, typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canInsertColumns<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_compatible_column_range<C> = true > bool QRangeModelAdapter::insertColumns(int before, C &&data)

Inserts columns constructed from the elements in data before the column specified by before into all rows, and returns whether the insertion was successful. If before is the same value as columnCount(), then the column will be appended to each row.

If the elements in data are values, then the new entries in all rows will be constructed from those values.

If the elements in data are containers, then the entries in the outer container will be used sequentially to construct the new entries for each subsequent row. If there are fewer elements in data than there are rows, then the function wraps around and starts again from the first element.

#### Constraints

Participates in overload resolution only if `Range` has rows that support insertion of elements, and the elements can be constructed from the entries in data.

See also removeColumns(), insertColumn(), and insertRows().

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canInsertRows<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true > bool QRangeModelAdapter::insertRow(QSpan<const int> before)

Inserts a single empty row before the row at the path specified by before, and returns whether the insertion was successful. If before is the same value as rowCount(), then the new row will be appended.

#### Constraints

Participates in overload resolution only if `Range` is a tree that supports insertion of elements.

This is an overloaded function.

See also insertRows(), removeRow(), and insertColumn().

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canInsertRows<I> = true> bool QRangeModelAdapter::insertRow(int before)

Inserts a single empty row before the row at before, and returns whether the insertion was successful. If before is the same value as rowCount(), then the new row will be appended.

#### Constraints

Participates in overload resolution only if `Range` supports insertion of elements.

This is an overloaded function.

See also insertRows(), removeRow(), and insertColumn().

### template < typename D, typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canInsertRows<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_compatible_row<D> = true, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true > bool QRangeModelAdapter::insertRow(QSpan<const int> before, D &&data)

Inserts a single row constructed from data before the row at before, and returns whether the insertion was successful. If before is the same value as rowCount(), then the new row will be appended.

#### Constraints

Participates in overload resolution only if `Range` is a tree that supports insertion of elements, and if a row can be constructed from data.

This is an overloaded function.

See also insertRows(), removeRow(), and insertColumn().

### template < typename D, typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canInsertRows<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_compatible_row<D> = true > bool QRangeModelAdapter::insertRow(int before, D &&data)

Inserts a single row constructed from data before the row at before, and returns whether the insertion was successful. If before is the same value as rowCount(), then the new row will be appended.

#### Constraints

Participates in overload resolution only if `Range` supports insertion of elements, and if a row can be constructed from data.

This is an overloaded function.

See also insertRows(), removeRow(), and insertColumn().

### template < typename C, typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canInsertRows<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_compatible_row_range<C> = true, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true > bool QRangeModelAdapter::insertRows(QSpan<const int> before, C &&data)

Inserts rows constructed from the elements in data before the row at before, and returns whether the insertion was successful. If before is the same value as rowCount(), then the new row will be appended.

#### Constraints

Participates in overload resolution only if `Range` is a tree that supports insertion of elements, and if rows can be constructed from the elements in data.

This is an overloaded function.

See also insertRow(), removeRows(), and insertColumns().

### template < typename C, typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canInsertRows<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_compatible_row_range<C> = true > bool QRangeModelAdapter::insertRows(int before, C &&data)

Inserts rows constructed from the elements in data before the row at before, and returns whether the insertion was successful. If before is the same value as rowCount(), then the new row will be appended.

#### Constraints

Participates in overload resolution only if `Range` supports insertion of elemnets, and if rows can be constructed from the elements in data.

This is an overloaded function.

See also insertRow(), removeRows(), and insertColumns().

### Model *QRangeModelAdapter::model() const

Returns the QRangeModel instance created by this adapter.

See also range() and at().

### template <typename F, QRangeModelAdapter<Range, Protocol, Model>::if_canMoveItems<F> = true> bool QRangeModelAdapter::moveColumn(int from, int to)

Moves the column at from to the column at to, and returns whether the column was successfully moved.

#### Constraints

Participates in overload resolution only if `Range` has rows that support moving of elements.

See also insertColumn(), removeColumn(), moveColumns(), and moveRow().

### template <typename F, QRangeModelAdapter<Range, Protocol, Model>::if_canMoveItems<F> = true> bool QRangeModelAdapter::moveColumns(int from, int count, int to)

Moves count columns starting at from to the position at to, and returns whether the columns were successfully moved.

#### Constraints

Participates in overload resolution only if `Range` has rows that support moving of elements.

See also insertColumns(), removeColumns(), moveColumn(), and moveRows().

### template < typename I, typename F, QRangeModelAdapter<Range, Protocol, Model>::if_canMoveItems<F> = true, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true > bool QRangeModelAdapter::moveRow(QSpan<const int> source, QSpan<const int> destination)

Moves the tree branch at source to the position at destination, and returns whether the branch was successfully moved.

#### Constraints

Participates in overload resolution only if `Range` is a tree that supports moving of elements.

This is an overloaded function.

See also insertRow(), removeRow(), and moveColumn().

### template <typename F, QRangeModelAdapter<Range, Protocol, Model>::if_canMoveItems<F> = true> bool QRangeModelAdapter::moveRow(int source, int destination)

Moves the row at source to the position at destination, and returns whether the row was successfully moved.

#### Constraints

Participates in overload resolution only if `Range` supports moving of elements.

This is an overloaded function.

See also insertRow(), removeRow(), and moveColumn().

### template < typename I, typename F, QRangeModelAdapter<Range, Protocol, Model>::if_canMoveItems<F> = true, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true > bool QRangeModelAdapter::moveRows(QSpan<const int> source, int count, QSpan<const int> destination)

Moves count tree branches starting at source to the position at destination, and returns whether the rows were successfully moved.

#### Constraints

Participates in overload resolution only if `Range` is a tree that supports moving of elements.

This is an overloaded function.

See also insertRows(), removeRows(), and moveColumns().

### template <typename F, QRangeModelAdapter<Range, Protocol, Model>::if_canMoveItems<F> = true> bool QRangeModelAdapter::moveRows(int source, int count, int destination)

Moves count rows starting at source to the position at destination, and returns whether the rows were successfully moved.

#### Constraints

Participates in overload resolution only if `Range` supports moving of elements.

This is an overloaded function.

See also insertRows(), removeRows(), and moveColumns().

### const QRangeModelAdapter<Range, Protocol, Model>::range_type &QRangeModelAdapter::range() const

Returns a const reference to the range that the model adapter operates on.

See also assign(), at(), and model().

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canRemoveColumns<I> = true> bool QRangeModelAdapter::removeColumn(int column)

Removes the given column from each row, and returns whether the removal was successful.

#### Constraints

Participates in overload resolution only if `Range` has rows that support removal of elements.

See also insertColumn(), removeColumns(), and removeRow().

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canRemoveColumns<I> = true> bool QRangeModelAdapter::removeColumns(int column, int count)

Removes count columns starting by the given column from each row, and returns whether the removal was successful.

#### Constraints

Participates in overload resolution only if `Range` has rows that support removal of elements.

See also insertColumns(), removeColumn(), and removeRow().

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canRemoveRows<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true > bool QRangeModelAdapter::removeRow(QSpan<const int> path)

Removes the row at the given path, including all children of that row, and returns whether the removal was successful.

#### Constraints

Participates in overload resolution only if `Range` is a tree that supports the removal of elements.

This is an overloaded function.

See also removeRows(), removeColumn(), and insertRow().

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canRemoveRows<I> = true> bool QRangeModelAdapter::removeRow(int row)

Removes the given row and returns whether the removal was successful.

#### Constraints

Participates in overload resolution only if `Range` supports the removal of elements.

This is an overloaded function.

See also removeRows(), removeColumn(), and insertRow().

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canRemoveRows<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true > bool QRangeModelAdapter::removeRows(QSpan<const int> path, int count)

Removes count rows starting at the row specified by path, and returns whether the removal was successful.

#### Constraints

Participates in overload resolution only if `Range` is a tree that supports the removal of elements.

This is an overloaded function.

See also removeRow(), removeColumns(), and insertRows().

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_canRemoveRows<I> = true> bool QRangeModelAdapter::removeRows(int row, int count)

Removes count rows starting at row, and returns whether the removal was successful.

#### Constraints

Participates in overload resolution only if `Range` supports the removal of elements.

This is an overloaded function.

See also removeRow(), removeColumns(), and insertRows().

### int QRangeModelAdapter::rowCount() const

Returns the number of rows. If the `Range` represents a list or table, then this is the number of rows. For trees, this is the number of top-level rows.

This is an overloaded function.

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true> int QRangeModelAdapter::rowCount(int row) const

### template <typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true> int QRangeModelAdapter::rowCount(QSpan<const int> row) const

Returns the number of rows under row.

#### Constraints

Participates in overload resolution only if `Range` is a tree.

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_list<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > bool QRangeModelAdapter::setData(int row, const QVariant &value, int role = Qt::EditRole)

Sets the role data for the item at row to value.

Returns `true` if successful; otherwise returns `false`.

#### Constraints

Participates in overload resolution only if `Range` is a mutable list.

See also data() and at().

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::if_tree<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > bool QRangeModelAdapter::setData(QSpan<const int> path, int column, const QVariant &value, int role = Qt::EditRole)

Sets the role data for the item referred to by path and column to value.

Returns `true` if successful; otherwise returns `false`.

#### Constraints

Participates in overload resolution only if `Range` is a mutable tree.

See also data() and at().

### template < typename I, QRangeModelAdapter<Range, Protocol, Model>::unless_list<I> = true, QRangeModelAdapter<Range, Protocol, Model>::if_writable<I> = true > bool QRangeModelAdapter::setData(int row, int column, const QVariant &value, int role = Qt::EditRole)

Sets the role data for the item referred to by row and column to value.

Returns `true` if successful; otherwise returns `false`.

#### Constraints

Participates in overload resolution only if `Range` is mutable, and not a list.

See also data() and at().

## Related Non-Members

### `[noexcept]` bool operator!=(const QRangeModelAdapter<Range, Protocol, Model> &lhs, const QRangeModelAdapter<Range, Protocol, Model> &rhs)

Returns whether lhs is not equal to rhs. Two adapters are equal if they both hold the same model instance.

### `[noexcept]` bool operator==(const QRangeModelAdapter<Range, Protocol, Model> &lhs, const QRangeModelAdapter<Range, Protocol, Model> &rhs)

Returns whether lhs is equal to rhs. Two adapters are equal if they both hold the same model instance.