1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite 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 TESTCPPMODELS_H
30#define TESTCPPMODELS_H
31
32#include <QAbstractListModel>
33#include <QVariant>
34#include <QtGui/private/qguiapplication_p.h>
35#include <QtQml/QQmlEngine>
36#include <QtQml/QJSEngine>
37
38class TestObject : public QObject
39{
40 Q_OBJECT
41 Q_PROPERTY(int value READ value CONSTANT)
42
43public:
44 TestObject(int val = 0) : m_value(val) {}
45 int value() const { return m_value; }
46private:
47 int m_value;
48};
49
50class TestItemModel : public QAbstractListModel
51{
52 Q_OBJECT
53
54public:
55 explicit TestItemModel(QObject *parent = 0)
56 : QAbstractListModel(parent) {}
57
58 enum {
59 TestRole = Qt::UserRole + 1
60 };
61
62 Q_INVOKABLE QVariant dataAt(int index) const
63 {
64 return QString("Row %1").arg(a: index);
65 }
66
67 QVariant data(const QModelIndex &index, int role) const
68 {
69 if (role == TestRole)
70 return dataAt(index: index.row());
71 else
72 return QVariant();
73 }
74
75 int rowCount(const QModelIndex & /*parent*/) const
76 {
77 return 10;
78 }
79
80 QHash<int, QByteArray> roleNames() const
81 {
82 QHash<int, QByteArray> rn = QAbstractItemModel::roleNames();
83 rn[TestRole] = "test";
84 return rn;
85 }
86
87private:
88 QList<TestObject> m_testobject;
89};
90
91class TestFetchAppendModel : public QAbstractListModel
92{
93 Q_OBJECT
94public:
95 explicit TestFetchAppendModel(QObject *parent = 0)
96 : QAbstractListModel(parent) { }
97
98 virtual int rowCount(const QModelIndex &parent) const
99 {
100 if (parent.isValid()) {
101 return 0;
102 }
103
104 return m_data.size();
105 }
106 virtual QVariant data(const QModelIndex &index, int role) const
107 {
108 if (!index.isValid() || role != Qt::DisplayRole) {
109 return QVariant();
110 }
111
112 return QVariant::fromValue(value: index.row());
113 }
114
115 virtual bool canFetchMore(const QModelIndex &parent) const
116 {
117 Q_UNUSED(parent)
118
119 return true;
120 }
121 virtual void fetchMore(const QModelIndex & parent)
122 {
123 Q_UNUSED(parent)
124
125 addMoreData();
126 }
127
128private:
129 void addMoreData()
130 {
131 static const int insertCount = 20;
132
133 beginInsertRows(parent: QModelIndex(), first: m_data.size(), last: m_data.size() + insertCount - 1);
134 for (int i = 0; i < insertCount; i++) {
135 m_data.append(t: int());
136 }
137 endInsertRows();
138 }
139
140 QList<int> m_data;
141};
142
143
144#endif // TESTCPPMODELS_H
145
146

source code of qtquickcontrols/tests/auto/testplugin/testcppmodels.h