-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCarModel.cpp
More file actions
88 lines (79 loc) · 1.82 KB
/
CarModel.cpp
File metadata and controls
88 lines (79 loc) · 1.82 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "CarModel.h"
#include <QDebug>
// USE THIS CONSTRUCTOR ONLY TO PROVIDE PROTOTYPE FOR MODEL CONSTRUCTOR
CarModel::CarModel(QObject *parent) : Models::ListItem(parent)
{
}
CarModel::CarModel(int id, const QString &name, const QString &brand, int price, QObject *parent) : Models::ListItem(parent),
carId(id),
name(name),
brand(brand),
price(price)
{
}
int CarModel::id() const
{
return this->car_id;
}
QVariant CarModel::data(int role) const
{
switch(role)
{
case car_name:
return this->name;
case car_brand:
return this->brand;
case car_price:
return this->price;
case car_id:
return this->id();
default :
QVariant();
}
}
bool CarModel::setData(int role, const QVariant &value)
{
switch(role)
{
case car_name:
this->name = value.toString();
// TO REFRESH MODEL
this->triggerItemUpdate();
return true;
case car_brand:
this->brand = value.toString();
// TO REFRESH MODEL
this->triggerItemUpdate();
return true;
case car_price:
this->price = value.toInt();
// TO REFRESH MODEL
qDebug() << "Price Changed" << this->price;
this->triggerItemUpdate();
return true;
default :
return false;
}
}
// ASSIGN THE NAME TO USE FROM QML SIDE TO ACCESS VALUES
QHash<int, QByteArray> CarModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[car_name] = "car_name";
roles[car_brand] = "car_brand";
roles[car_price] = "car_price";
roles[car_id] = "car_id";
return roles;
}
void CarModel::setCarName(const QString &carName)
{
this->name = carName;
}
void CarModel::setCarBrand(const QString &carBrand)
{
this->brand = carBrand;
}
void CarModel::setCarPrice(int price)
{
this->price = price;
}