Skip to content

Commit 10a0228

Browse files
committed
- JObject class enhanced for MetaProperty*Ptr_List
- Example updated as well
1 parent 9b694de commit 10a0228

File tree

4 files changed

+60
-51
lines changed

4 files changed

+60
-51
lines changed

README.md

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ See the test project in test directory for a bit more details.
2424

2525
Usage
2626
======
27-
Improting JSON to your class's instance!
28-
===
29-
// Importing json into your custom instance
30-
QByteArray jsonInput = "\"{\"address\": {"
27+
Improting JSON to your class's instance!
28+
======
29+
// Importing json into your custom instance
30+
QByteArray jsonInput = "{\"address\": {"
3131
"\"city\": \"Rajshahi\","
3232
"\"country\": \"Bangladesh\","
3333
"\"zip\": \"6402\"},"
@@ -49,35 +49,37 @@ Usage
4949
"\"name\": \"Mirza Alam\","
5050
"\"speciality\": ["
5151
"\"C/C++\","
52-
"\"Algorithms\"]}]}\"";
52+
"\"Algorithms\"]}]}";
5353

54-
School* school = new School(&a);
55-
school->importFromJson(jsonInput);
56-
qDebug() <<"School name: " << school->name();
57-
qDebug() <<"School address's city: " << school->address()->city();
54+
School school;
55+
school.importFromJson(jsonInput);
56+
qDebug() <<"School name: " << school.name();
57+
qDebug() <<"School address's city: " << school.address()->city();
58+
qDebug() <<"Teacer name: " << school.itemTeacherAt(0)->name();
59+
qDebug() <<"Student name: " << school.itemStudentAt(0)->name();
5860

5961

60-
Exporting JSON from your class's instance!
61-
===
62-
School* school = new School(&a);
63-
school->name("Kernel Coders Lab --- www.kernelcoderslab.com");
64-
Address* address = new Address(school);
65-
address->country("Bangladesh");address->city("Rajshahi");address->zip("6402");
66-
school->address(address);
62+
Exporting JSON from your class's instance!
63+
======
64+
School school;
65+
school.name("Kernel Coders Lab --- www.kernelcoderslab.com");
66+
Address* address = new Address(&school);
67+
address->country("Bangladesh");address->city("Rajshahi");address->zip("6402");
68+
school.address(address);
6769

68-
Teacher* teacher = new Teacher(school);
69-
teacher->name("Mirza Alam");teacher->speciality(QStringList() << "C/C++" << "Algorithms");
70-
address = new Address(teacher);
71-
address->country("Bangladesh");address->city("Dhaka");address->zip("1000");
72-
teacher->address(address);
73-
school->appendTeacher(teacher);
70+
Teacher* teacher = new Teacher(&school);
71+
teacher->name("Mirza Alam");teacher->speciality(QStringList() << "C/C++" << "Algorithms");
72+
address = new Address(teacher);
73+
address->country("Bangladesh");address->city("Dhaka");address->zip("1000");
74+
teacher->address(address);
75+
school.appendTeacher(teacher);
7476

75-
Student* student = new Student(school);
76-
student->name("Shahazan Ali");student->courses(QStringList() << "C/C++" << "Algorithms");
77-
address = new Address(student);
78-
address->country("Bangladesh");address->city("Natore");address->zip("6200");
79-
student->address(address);
80-
school->appendStudent(student);
77+
Student* student = new Student(&school);
78+
student->name("Shahazan Ali");student->courses(QStringList() << "C/C++" << "Algorithms");
79+
address = new Address(student);
80+
address->country("Bangladesh");address->city("Natore");address->zip("6200");
81+
student->address(address);
82+
school.appendStudent(student);
8183

82-
QByteArray json = school->exportToJson();
83-
qDebug() << json;
84+
QByteArray json = school.exportToJson();
85+
qDebug() << json;

src/JObject.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ bool JObject::importFromVariant(const QVariant &v)
9595
if (child) {
9696
child->setParent(this);
9797
child->importFromVariant(value);
98+
setProperty(mp.name(), QVariant::fromValue(child));
9899
}
99100
else {
100101
return false;

src/JObject.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,17 @@
6161
#define MetaPropertyPrivateSet_Ptr_List(t, x) private: Q_PROPERTY(QVariantList x READ x WRITE x) \
6262
PropertyPrivateSet(QVariantList, x) \
6363
public: Q_INVOKABLE void _type##x(const QString& prop){registerPtrProperty(prop, &t::staticMetaObject);} \
64-
void append##t(t* i){QVariant v = QVariant::fromValue(i); if (v.isValid()) { _##x.append(v);}}
64+
void append##t(t* i){QVariant v = QVariant::fromValue(i); if (v.isValid()) { _##x.append(v);}} \
65+
void remove##t(t* i){QVariant v = QVariant::fromValue(i); if (v.isValid()) { _##x.removeAll(v);}}
6566

6667
// t: type, x: property name
6768
#define MetaPropertyPublicSet_Ptr_List(t, x) private: Q_PROPERTY(QVariantList x READ x WRITE x) \
6869
PropertyPublicSet(QVariantList, x) \
6970
public: Q_INVOKABLE void _type##x(const QString& prop){registerPtrProperty(prop, &t::staticMetaObject);} \
70-
void append##t(t* i){QVariant v = QVariant::fromValue(i); if (v.isValid()) { _##x.append(v);}}
71+
void append##t(t* i){QVariant v = QVariant::fromValue(i); if (v.isValid()) { _##x.append(v);}} \
72+
void remove##t(t* i){QVariant v = QVariant::fromValue(i); if (v.isValid()) { _##x.removeAll(v);}} \
73+
int count##t()const {return _##x.length();} \
74+
t* item##t##At(int i) {return _##x.at(i).value<t*>();}
7175

7276

7377
/**

test/main.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,34 @@
55
int main(int argc, char *argv[])
66
{
77
QCoreApplication a(argc, argv);
8-
9-
School* school = new School(&a);
10-
school->name("Kernel Coders Lab --- www.kernelcoderslab.com");
11-
Address* address = new Address(school);
8+
{
9+
School school;
10+
school.name("Kernel Coders Lab --- www.kernelcoderslab.com");
11+
Address* address = new Address(&school);
1212
address->country("Bangladesh");address->city("Rajshahi");address->zip("6402");
13-
school->address(address);
13+
school.address(address);
1414

15-
Teacher* teacher = new Teacher(school);
15+
Teacher* teacher = new Teacher(&school);
1616
teacher->name("Mirza Alam");teacher->speciality(QStringList() << "C/C++" << "Algorithms");
1717
address = new Address(teacher);
1818
address->country("Bangladesh");address->city("Dhaka");address->zip("1000");
1919
teacher->address(address);
20-
school->appendTeacher(teacher);
20+
school.appendTeacher(teacher);
2121

22-
Student* student = new Student(school);
22+
Student* student = new Student(&school);
2323
student->name("Shahazan Ali");student->courses(QStringList() << "C/C++" << "Algorithms");
2424
address = new Address(student);
2525
address->country("Bangladesh");address->city("Natore");address->zip("6200");
2626
student->address(address);
27-
school->appendStudent(student);
27+
school.appendStudent(student);
2828

29-
QByteArray json = school->exportToJson();
29+
QByteArray json = school.exportToJson();
3030
qDebug() << json;
31+
}
3132

32-
33-
33+
{
3434
// Importing json into your custom instance
35-
QByteArray jsonInput = "\"{\"address\": {"
35+
QByteArray jsonInput = "{\"address\": {"
3636
"\"city\": \"Rajshahi\","
3737
"\"country\": \"Bangladesh\","
3838
"\"zip\": \"6402\"},"
@@ -54,12 +54,14 @@ int main(int argc, char *argv[])
5454
"\"name\": \"Mirza Alam\","
5555
"\"speciality\": ["
5656
"\"C/C++\","
57-
"\"Algorithms\"]}]}\"";
58-
59-
School* school = new School(&a);
60-
school->importFromJson(jsonInput);
61-
qDebug() <<"School name: " << school->name();
62-
qDebug() <<"School address's city: " << school->address()->city();
57+
"\"Algorithms\"]}]}";
6358

59+
School school;
60+
school.importFromJson(jsonInput);
61+
qDebug() <<"School name: " << school.name();
62+
qDebug() <<"School address's city: " << school.address()->city();
63+
qDebug() <<"Teacer name: " << school.itemTeacherAt(0)->name();
64+
qDebug() <<"Student name: " << school.itemStudentAt(0)->name();
65+
}
6466
return a.exec();
6567
}

0 commit comments

Comments
 (0)