-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
59 lines (45 loc) · 1.43 KB
/
main.cpp
File metadata and controls
59 lines (45 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
// QStandardItemModel derived class
#include <QStandardItemModel>
class MyModel : public QStandardItemModel {
public:
enum Role {
role1=Qt::UserRole,
role2,
role3
};
explicit MyModel(QObject * parent = 0): QStandardItemModel(parent){}
explicit MyModel( int rows, int columns, QObject * parent = 0 )
: QStandardItemModel(rows, columns, parent){}
QHash<int, QByteArray> roleNames() const{
QHash<int, QByteArray> roles;
roles[role1] = "one";
roles[role2] = "two";
roles[role3] = "three";
return roles;
}
};
// Main
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyModel* model = new MyModel;
QStandardItem* it = new QStandardItem();
it->setData("data 1", MyModel::role1);
it->setData("data 2", MyModel::role2);
it->setData("data 3", MyModel::role3);
model->appendRow(it);
it = new QStandardItem();
it->setData("more data 1", MyModel::role1);
it->setData("more data 2", MyModel::role2);
it->setData("more data 3", MyModel::role3);
model->appendRow(it);
QQmlApplicationEngine engine;
// register cpp model with QML engine
engine.rootContext()->setContextProperty("myModel", model);
// Load qml file in QML engine
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}