1/****************************************************************************
2**
3** Copyright (C) 2013 Dmitrii Kosarev aka Kakadu <kakadu.hafanana@gmail.com>
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#ifndef STRINGMODEL_H
30#define STRINGMODEL_H
31
32#include <QtCore/QObject>
33#include <QtCore/QAbstractItemModel>
34#include <QtCore/QDebug>
35
36class StringModel : public QAbstractItemModel
37{
38 Q_OBJECT
39 QVector<QString> items;
40 QHash<int, QByteArray> roles;
41 QString name;
42
43public:
44 explicit StringModel(const QString& name) : QAbstractItemModel(), name(name)
45 {
46 roles.insert(akey: 555, avalue: "text");
47 }
48
49 void drop(int count)
50 {
51 beginRemoveRows(parent: QModelIndex(), first: 0, last: count-1);
52 for (int i=0; i<count; i++)
53 items.pop_front();
54 endRemoveRows();
55 }
56
57 Q_INVOKABLE void add(QString s)
58 {
59 beginInsertRows(parent: QModelIndex(), first: 0, last: 0);
60 items.push_front(t: s);
61 endInsertRows();
62 }
63
64 int rowCount(const QModelIndex &) const override
65 {
66 return items.count();
67 }
68
69 QHash<int, QByteArray> roleNames() const override
70 {
71 return roles;
72 }
73
74 int columnCount(const QModelIndex &) const override
75 {
76 return 1;
77 }
78
79 bool hasChildren(const QModelIndex &) const override
80 {
81 return rowCount(QModelIndex()) > 0;
82 }
83
84 QModelIndex index(int row, int column, const QModelIndex &parent) const override
85 {
86 Q_UNUSED(column);
87 if (row>=0 && row<rowCount(parent))
88 return createIndex(arow: row,acolumn: 0);
89 else
90 return QModelIndex();
91 }
92
93 QModelIndex parent(const QModelIndex &) const override
94 {
95 return QModelIndex();
96 }
97
98 QVariant data (const QModelIndex & index, int role) const override
99 {
100 int row = index.row();
101 if ((row<0) || (row>=items.count()))
102 return QVariant::Invalid;
103
104 switch (role) {
105 case Qt::DisplayRole:
106 case 555:
107 return QVariant::fromValue(value: items.at(i: row));
108 default:
109 return QVariant();
110 }
111 }
112};
113
114#endif // STRINGMODEL_H
115

source code of qt3d/tests/auto/quick3d/quick3dnodeinstantiator/stringmodel.h