Lomiri
Loading...
Searching...
No Matches
QVariantListModel Class Reference

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

Public Member Functions

 QVariantListModel (QObject *parent=0)
 
 QVariantListModel (const QVariantList &list, QObject *parent=0)
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
QModelIndex sibling (int row, int column, const QModelIndex &idx) const override
 
QVariant data (const QModelIndex &index, int role) const override
 
bool setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
 
bool insertRows (int row, int count, const QModelIndex &parent=QModelIndex()) override
 
bool removeRows (int row, int count, const QModelIndex &parent=QModelIndex()) override
 
QHash< int, QByteArray > roleNames () const override
 
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 \l{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() [1/2]

QVariantListModel::QVariantListModel ( QObject * parent = 0)
explicit

Constructs a variant list model with the given parent.

Definition at line 87 of file qvariantlistmodel.cpp.

87 :
88 QAbstractListModel(parent)
89{
90 this->roles = QAbstractListModel::roleNames();
91 this->roles[Qt::DisplayRole] = "modelData";
92}

◆ QVariantListModel() [2/2]

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 99 of file qvariantlistmodel.cpp.

99 :
100 QAbstractListModel(parent), lst(list)
101{
102 this->roles = QAbstractListModel::roleNames();
103 this->roles[Qt::DisplayRole] = "modelData";
104}

◆ ~QVariantListModel()

QVariantListModel::~QVariantListModel ( )

Definition at line 106 of file qvariantlistmodel.cpp.

106 {
107}

Member Function Documentation

◆ data()

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

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 154 of file qvariantlistmodel.cpp.

155{
156 if (index.row() < 0 || index.row() >= lst.size())
157 return QVariant();
158
159 if (role == Qt::DisplayRole || role == Qt::EditRole)
160 return lst.at(index.row());
161
162 return QVariant();
163}

◆ insertRows()

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

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 198 of file qvariantlistmodel.cpp.

200{
201 if (count < 1 || row < 0 || row > rowCount(parent))
202 return false;
203
204 beginInsertRows(QModelIndex(), row, row + count - 1);
205
206 for (int r = 0; r < count; ++r)
207 lst.insert(row, QVariant());
208
209 endInsertRows();
210
211 return true;
212}

◆ removeRows()

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

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 225 of file qvariantlistmodel.cpp.

227{
228 if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
229 return false;
230
231 beginRemoveRows(QModelIndex(), row, row + count - 1);
232
233 for (int r = 0; r < count; ++r)
234 lst.removeAt(row);
235
236 endRemoveRows();
237
238 return true;
239}

◆ roleNames()

QHash< int, QByteArray > QVariantListModel::roleNames ( ) const
override

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 120 of file qvariantlistmodel.cpp.

121{
122 return this->roles;
123}

◆ rowCount()

int QVariantListModel::rowCount ( const QModelIndex & parent = QModelIndex()) const
override

Definition at line 125 of file qvariantlistmodel.cpp.

126{
127 if (parent.isValid())
128 return 0;
129
130 return lst.count();
131}

◆ setData()

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

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 174 of file qvariantlistmodel.cpp.

176{
177 if (index.row() >= 0 && index.row() < lst.size()
178 && (role == Qt::EditRole || role == Qt::DisplayRole))
179 {
180 lst.replace(index.row(), value);
181 dataChanged(index, index, QVector<int>() << role);
182 return true;
183 }
184 return false;
185}

◆ setVariantList()

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 255 of file qvariantlistmodel.cpp.

256{
257 int size = lst.size();
258 bool sameSize = list.size() == size;
259 if (!sameSize)
260 {
261 beginResetModel();
262 }
263 lst = list;
264 if (!sameSize)
265 {
266 endResetModel();
267 } else
268 {
269 dataChanged(QAbstractListModel::index(0),
270 QAbstractListModel::index(size - 1));
271 }
272}

◆ sibling()

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

\reimp

Definition at line 136 of file qvariantlistmodel.cpp.

138{
139 if (!idx.isValid() || column != 0 || row >= lst.count())
140 return QModelIndex();
141
142 return createIndex(row, 0);
143}

◆ variantList()

QVariantList QVariantListModel::variantList ( ) const

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

Definition at line 244 of file qvariantlistmodel.cpp.

245{
246 return lst;
247}

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