All Classes Functions Properties
QVariantListModel Class Reference

The QVariantListModel class provides a model that supplies variants to views. More...

Inherits QAbstractListModel.

## Public Member Functions

| QVariantListModel (QObject *parent=0)
| QVariantListModel (const QVariantList &list, QObject *parent=0)
int | rowCount (const QModelIndex &parent=QModelIndex()) const
QModelIndex | sibling (int row, int column, const QModelIndex &idx) const
QVariant | data (const QModelIndex &index, int role) const
bool | setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
bool | insertRows (int row, int count, const QModelIndex &parent=QModelIndex())
bool | removeRows (int row, int count, const QModelIndex &parent=QModelIndex())
QVariantList | variantList () const
void | setVariantList (const QVariantList &list)

## Detailed Description

The QVariantListModel class provides a model that supplies variants to views.

QVariantListModel is an editable model that can be used for simple cases where you need to display a number of variants in a view.

The model provides all the standard functions of an editable model, representing the data in the variant list as a model with one column and a number of rows equal to the number of items in the list.

Model indexes corresponding to items are obtained with the {QAbstractListModel::index()}{index()} function. Item data is read with the data() function and written with setData(). The number of rows (and number of items in the variant list) can be found with the rowCount() function.

The model can be constructed with an existing variant list, or variants can be set later with the setVariantList() convenience function. Variants can also be inserted in the usual way with the insertRows() function, and removed with removeRows(). The contents of the variant list can be retrieved with the variantList() convenience function.

See Also
  QAbstractListModel, QAbstractItemModel, {Model Classes}

Definition at line 48 of file qvariantlistmodel.h.

## Constructor & Destructor Documentation

QVariantListModel::QVariantListModel | ( | QObject * | parent = `0` | ) |

explicit

Constructs a variant list model with the given parent.

Definition at line 87 of file qvariantlistmodel.cpp.

:
QAbstractListModel(parent)
{
QHash<int, QByteArray> roles(roleNames());
roles[Qt::DisplayRole] = "modelData";
setRoleNames(roles);
}

QVariantListModel::QVariantListModel | ( | const QVariantList & | list,
|  | QObject * | parent = `0`
| ) |  |

explicit

Constructs a variant list model containing the specified list with the given parent.

Definition at line 100 of file qvariantlistmodel.cpp.

:
QAbstractListModel(parent), lst(list)
{
QHash<int, QByteArray> roles(roleNames());
roles[Qt::DisplayRole] = "modelData";
setRoleNames(roles);
}

## Member Function Documentation

QVariant QVariantListModel::data | ( | const QModelIndex & | index,
|  | int | role
| ) |  | const

Returns data for the specified role, from the item with the given index.

If the view requests an invalid index, an invalid variant is returned.

See Also
  setData()

Definition at line 151 of file qvariantlistmodel.cpp.

{
if (index.row() < 0 || index.row() >= lst.size())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole)
return lst.at(index.row());
return QVariant();
}

bool QVariantListModel::insertRows | ( | int | row,
|  | int | count,
|  | const QModelIndex & | parent = `QModelIndex()`
| ) |  |

Inserts count rows into the model, beginning at the given row.

The parent index of the rows is optional and is only used for consistency with QAbstractItemModel. By default, a null index is specified, indicating that the rows are inserted in the top level of the model.

See Also
  QAbstractItemModel::insertRows()

Definition at line 195 of file qvariantlistmodel.cpp.

{
if (count < 1 || row < 0 || row > rowCount(parent))
return false;
beginInsertRows(QModelIndex(), row, row + count - 1);
for (int r = 0; r < count; ++r)
lst.insert(row, QVariant());
endInsertRows();
return true;
}
QVariantListModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: qvariantlistmodel.cpp:122

bool QVariantListModel::removeRows | ( | int | row,
|  | int | count,
|  | const QModelIndex & | parent = `QModelIndex()`
| ) |  |

Removes count rows from the model, beginning at the given row.

The parent index of the rows is optional and is only used for consistency with QAbstractItemModel. By default, a null index is specified, indicating that the rows are removed in the top level of the model.

See Also
  QAbstractItemModel::removeRows()

Definition at line 222 of file qvariantlistmodel.cpp.

{
if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
return false;
beginRemoveRows(QModelIndex(), row, row + count - 1);
for (int r = 0; r < count; ++r)
lst.removeAt(row);
endRemoveRows();
return true;
}
QVariantListModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: qvariantlistmodel.cpp:122

int QVariantListModel::rowCount | ( | const QModelIndex & | parent = `QModelIndex()` | ) | const

Returns the number of rows in the model. This value corresponds to the number of items in the model's internal variant list.

The optional parent argument is in most models used to specify the parent of the rows to be counted. Because this is a list if a valid parent is specified, the result will always be 0.

See Also
  insertRows(), removeRows(), QAbstractItemModel::rowCount()

Definition at line 122 of file qvariantlistmodel.cpp.

{
if (parent.isValid())
return 0;
return lst.count();
}

bool QVariantListModel::setData | ( | const QModelIndex & | index,
|  | const QVariant & | value,
|  | int | role = `Qt::EditRole`
| ) |  |

Sets the data for the specified role in the item with the given index in the model, to the provided value.

The dataChanged() signal is emitted if the item is changed.

See Also
  Qt::ItemDataRole, data()

Definition at line 171 of file qvariantlistmodel.cpp.

{
if (index.row() >= 0 && index.row() < lst.size()
&& (role == Qt::EditRole || role == Qt::DisplayRole))
{
lst.replace(index.row(), value);
dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}

void QVariantListModel::setVariantList | ( | const QVariantList & | list | ) |

Sets the model's internal variant list to list. The model will notify any attached views that its underlying data has changed.

See Also
  dataChanged()

Definition at line 252 of file qvariantlistmodel.cpp.

{
int size = lst.size();
bool sameSize = list.size() == size;
if (!sameSize)
{
beginResetModel();
}
lst = list;
if (!sameSize)
{
endResetModel();
} else
{
dataChanged(QAbstractListModel::index(0),
QAbstractListModel::index(size - 1));
}
}

QModelIndex QVariantListModel::sibling | ( | int | row,
|  | int | column,
|  | const QModelIndex & | idx
| ) |  | const

Definition at line 133 of file qvariantlistmodel.cpp.

{
if (!idx.isValid() || column != 0 || row >= lst.count())
return QModelIndex();
return createIndex(row, 0);
}

QVariantList QVariantListModel::variantList | ( |  | ) | const

Returns the variant list used by the model to store data.

Definition at line 241 of file qvariantlistmodel.cpp.

{
return lst;
}

The documentation for this class was generated from the following files:

- plugins/Utils/qvariantlistmodel.h
- plugins/Utils/qvariantlistmodel.cpp