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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| #ifndef WIDGET_H #define WIDGET_H
#include <QWidget> #include "qperson.h" namespace Ui { class Widget; }
class Widget : public QWidget { Q_OBJECT
public: explicit Widget(QWidget *parent = 0); ~Widget();
private: Ui::Widget *ui; QPerson * boy; QPerson * girl; private slots: void on_btnBoyInc_clicked(); void on_btnGirlInc_clicked(); void on_btnMetaInfo_clicked(); void onspin_valueChanged(int value); void onageChanged(unsigned value); };
#endif
#include "widget.h" #include "ui_widget.h" #include <QMetaProperty> #include <QMetaClassInfo> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); ui->spinBoxBoy->setProperty("isBoy",true); ui->spinBoxGirl->setProperty("isBoy",false); boy = new QPerson("王小明"); boy->setProperty("score",85); boy->setProperty("age",10); boy->setProperty("sex","Boy");
girl = new QPerson("小红"); girl->setProperty("score",85); girl ->setProperty("age",10); girl ->setProperty("sex","girl"); connect(boy,&QPerson::ageChanged,this,&Widget::onageChanged); connect(girl,&QPerson::ageChanged,this,&Widget::onageChanged); connect(ui->spinBoxBoy,SIGNAL(valueChanged(int)),this,SLOT(onspin_valueChanged(int))); connect(ui->spinBoxGirl,SIGNAL(valueChanged(int)),this,SLOT(onspin_valueChanged(int))); }
Widget::~Widget() { delete ui; }
void Widget::onspin_valueChanged(int value) { Q_UNUSED(value); QSpinBox* spinBox = qobject_cast<QSpinBox*>(sender()); if(spinBox->property("isBoy").toBool()) boy->setAge(spinBox->value()); else girl->setAge(spinBox->value());
}
void Widget::onageChanged(unsigned value) { Q_UNUSED(value); QPerson* per = qobject_cast<QPerson*>(sender()); QString name = per->property("name").toString(); QString sex = per->property("sex").toString(); unsigned age = per->age(); ui->txtEdit->appendPlainText(name+","+sex+","+QString::asprintf(",年龄=%d",age));
}
void Widget::on_btnBoyInc_clicked() { boy->incAge(); }
void Widget::on_btnGirlInc_clicked() { girl->incAge(); }
void Widget::on_btnMetaInfo_clicked() { const QMetaObject * meta = girl->metaObject(); ui->txtEdit->clear(); ui->txtEdit->appendPlainText("====元信息========"); ui->txtEdit->appendPlainText(QString("类名称:%1\n").arg(meta->className())); ui->txtEdit->appendPlainText(QString("property")); for(int i = meta->propertyOffset();i<meta->propertyCount();i++) { QMetaProperty prop=meta->property(i); const char* propName = prop.name(); QString propValue = boy->property(propName).toString(); ui->txtEdit->appendPlainText(QString("属性名称:%1,属性值:%2").arg(propName).arg(propValue)); } ui->txtEdit->appendPlainText(QString("")); ui->txtEdit->appendPlainText(QString("classInfo")); for(int i=meta->classInfoOffset();i<meta->classInfoCount();i++) { QMetaClassInfo info = meta->classInfo(i); ui->txtEdit->appendPlainText(QString("Name=%1,value=%2").arg(info.name()).arg(info.value())); } }
|