fork download
  1. //person.h
  2. #ifndef PERSON_H
  3. #define PERSON_H
  4.  
  5. #include <QObject>
  6. #include <QDebug>
  7. #include <QMetaObject>
  8. #include <QMetaEnum>
  9. #include <QString>
  10. #include <QDate>
  11. #include <QFile>
  12.  
  13. static const char* BLOODTYPE = "BloodType";
  14.  
  15. class Person : public QObject
  16. {
  17. Q_OBJECT
  18. Q_ENUMS(BloodType)
  19. Q_PROPERTY(QString name READ getName WRITE setName)
  20. Q_PROPERTY(QDate dob READ getDob WRITE setDob)
  21. Q_PROPERTY(Person::BloodType bloodtype READ getBloodType WRITE setBloodType)
  22.  
  23. public:
  24. enum BloodType {A,B,O};
  25. explicit Person(QObject *parent = nullptr,QString name="",QDate dob=QDate::currentDate(),BloodType bloodtype=BloodType::O);
  26.  
  27. static int getBloodTypeCount();
  28. static QString getBloodType(int index);
  29. static QString getBloodType(BloodType bloodtype);
  30. static BloodType getBloodType(QString bloodtype);
  31.  
  32. QString getName() const;
  33. void setName(const QString &name);
  34.  
  35. QDate getDob() const;
  36. void setDob(const QDate &dob);
  37.  
  38. BloodType getBloodType(); // get the blood type of this person
  39. void setBloodType(BloodType bloodtype); // set the blood type of this person
  40.  
  41. private:
  42. QString name;
  43. QDate dob;
  44. BloodType bloodtype;
  45. int bloodtypes;
  46.  
  47. signals:
  48.  
  49. };
  50.  
  51. #endif // PERSON_H
  52.  
  53.  
  54. //person.cpp
  55. #include "person.h"
  56.  
  57. Person::Person(QObject *parent, QString name, QDate dob, BloodType bloodtype) : QObject{parent}
  58. {
  59. setName(name);
  60. setDob(dob);
  61. setBloodType(bloodtype);
  62. }
  63.  
  64. int Person::getBloodTypeCount()
  65. {
  66. QMetaObject mo = Person::staticMetaObject;
  67. QMetaEnum me = mo.enumerator(mo.indexOfEnumerator(BLOODTYPE));
  68. return me.keyCount();
  69. }
  70.  
  71. QString Person::getBloodType(int index)
  72. {
  73. QMetaObject mo = Person::staticMetaObject;
  74. QMetaEnum me = mo.enumerator(mo.indexOfEnumerator(BLOODTYPE));
  75. return QString(me.key(index));
  76.  
  77. }
  78.  
  79. QString Person::getBloodType(BloodType bloodtype)
  80. {
  81. QMetaObject mo = Person::staticMetaObject;
  82. QMetaEnum me = mo.enumerator(mo.indexOfEnumerator(BLOODTYPE));
  83. return QString(me.key(bloodtype));
  84. }
  85.  
  86. Person::BloodType Person::getBloodType(QString bloodtype)
  87. {
  88. QMetaObject mo = Person::staticMetaObject;
  89. QMetaEnum me = mo.enumerator(mo.indexOfEnumerator(BLOODTYPE));
  90. bool OK=false;
  91. int val = me.keyToValue(bloodtype.toStdString().c_str(),&OK);
  92. //int val = me.keyToValue(bloodtype.toLatin1());
  93.  
  94. switch(val) {
  95. case BloodType::A:
  96. return BloodType::A;
  97. case BloodType::B:
  98. return BloodType::B;
  99. case BloodType::O:
  100. return BloodType::O;
  101. }
  102.  
  103. }
  104.  
  105. QString Person::getName() const
  106. {
  107. return name;
  108. }
  109.  
  110. void Person::setName(const QString &name)
  111. {
  112. this->name = name;
  113. }
  114.  
  115. QDate Person::getDob() const
  116. {
  117. return dob;
  118. }
  119.  
  120. void Person::setDob(const QDate &dob)
  121. {
  122. this->dob = dob;
  123. }
  124.  
  125. Person::BloodType Person::getBloodType()
  126. {
  127. return bloodtype;
  128. }
  129.  
  130. void Person::setBloodType(BloodType bloodtype)
  131. {
  132. this->bloodtype = bloodtype;
  133. }
  134.  
  135. //main.cpp
  136. #include <QCoreApplication>
  137. #include "person.h"
  138.  
  139. void writeToFile(QObject* obj)
  140. {
  141. QFile file("people.txt");
  142. file.open(QIODevice::Append);
  143. QTextStream stream(&file);
  144.  
  145. const QMetaObject* mo = obj->metaObject();
  146. for (int i = mo->propertyOffset(); i < mo->propertyCount(); i++)
  147. {
  148. const QMetaProperty mp = mo->property(i);
  149. //QString name = mp.name(); // this is the property name;
  150. const char* name = mp.name();
  151. if(mp.isEnumType()) {
  152. qInfo() << "propname: " << name << " isEnumType: true";
  153. QVariant valVar = obj->property(name);
  154. if(valVar.isNull())
  155. {
  156. qInfo() << "propname: " << name << " isNull: true";
  157. }
  158. else
  159. {
  160. qInfo() << "propname: " << name << " isNull: false";
  161. }
  162. if(valVar.isValid())
  163. {
  164. qInfo() << "propname: " << name << " isValid: true";
  165. }
  166. else
  167. {
  168. qInfo() << "propname: " << name << " isValid: false";
  169. }
  170. bool OK = false;
  171. int nVal = valVar.toInt(&OK);
  172. if(!OK) {
  173. qInfo() << "valVar.toInt(&OK): false";
  174. }
  175. else {
  176. qInfo() << "valVar.toInt(&OK): true";
  177. }
  178. Person::BloodType bt = valVar.value<Person::BloodType>();
  179. qInfo() << "valVar: " << valVar;
  180. qInfo() << "Person::getBloodType(bt): " << Person::getBloodType(bt);
  181. QString valStr = Person::getBloodType(bt);
  182. qInfo() << name << ": " << valStr;
  183. stream << name << ": " << valStr << "\n";
  184. }
  185. else
  186. {
  187. qInfo() << "propname: " << name << " isEnumType: false";
  188. //QVariant valVar = prop.read(obj); // property value stored in a QVariant variable value
  189. QVariant valVar = obj->property(name);
  190. QString valStr = valVar.toString(); //QVariant value converted to QString
  191. qInfo() << name << ": " << valStr << "\n";
  192. stream << name << ": " << valStr << "\n";
  193.  
  194. }
  195. }
  196. file.close();
  197. }
  198.  
  199. int main(int argc, char *argv[])
  200. {
  201. QCoreApplication a(argc, argv);
  202.  
  203. Person *bart = new Person(&a,"Bart Simpson",QDate::currentDate(),Person::BloodType::O);
  204.  
  205. writeToFile(bart);
  206.  
  207. return a.exec();
  208. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:5:10: fatal error: QObject: No such file or directory
 #include <QObject>
          ^~~~~~~~~
compilation terminated.
stdout
Standard output is empty