init study Qt
This commit is contained in:
100
widget.cpp
Normal file
100
widget.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "widget.h"
|
||||
#include "ui_widget.h"
|
||||
#include "qperson.h"
|
||||
#include "QMetaProperty"
|
||||
|
||||
Widget::Widget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, ui(new Ui::Widget)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
boy = new QPerson("南街");
|
||||
boy->setProperty("score", 99);
|
||||
boy->setProperty("age", 10);
|
||||
boy->setProperty("sex", "Boy"); //动态属性
|
||||
ui->spinBoy->setValue(10);
|
||||
connect(boy, &QPerson::ageChanged, this, &Widget::on_ageChanged);
|
||||
|
||||
girl = new QPerson("小丽");
|
||||
girl->setProperty("score", 66);
|
||||
girl->setProperty("age", 6);
|
||||
girl->setProperty("sex", "Girl"); //动态属性
|
||||
ui->spinGirl->setValue(6);
|
||||
connect(girl, &QPerson::ageChanged, this, &Widget::on_ageChanged);
|
||||
|
||||
ui->spinBoy->setProperty("isBoy", true); // ui动态属性
|
||||
ui->spinGirl->setProperty("isBoy", false);
|
||||
|
||||
connect(ui->spinBoy, SIGNAL(valueChanged(int)),
|
||||
this, SLOT(on_spin_valueChanged(int)));
|
||||
connect(ui->spinGirl, SIGNAL(valueChanged(int)),
|
||||
this, SLOT(on_spin_valueChanged(int)));
|
||||
}
|
||||
|
||||
Widget::~Widget()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void Widget::on_ageChanged(int val)
|
||||
{
|
||||
Q_UNUSED(val)
|
||||
QPerson *aPerson = qobject_cast<QPerson*>(sender());
|
||||
QString hisName = aPerson->property("name").toString();
|
||||
QString hisSex = aPerson->property("sex").toString();
|
||||
int hisAge = aPerson->age();
|
||||
hisAge = aPerson->property("age").toInt();
|
||||
ui->textEdit->append(hisName + ", " + hisSex + QString::asprintf(", 年龄=%d", hisAge));
|
||||
}
|
||||
void Widget::on_spin_valueChanged(int arg1)
|
||||
{
|
||||
Q_UNUSED(arg1)
|
||||
auto spinBox = qobject_cast<QSpinBox*>(sender());
|
||||
if(spinBox->property("isBoy").toBool())
|
||||
{
|
||||
boy->setAge(arg1);
|
||||
}
|
||||
else
|
||||
{
|
||||
girl->setAge(spinBox->value());
|
||||
}
|
||||
}
|
||||
void Widget::on_btnClear_clicked()
|
||||
{
|
||||
ui->textEdit->clear();
|
||||
}
|
||||
void Widget::on_btnBoyInc_clicked()
|
||||
{
|
||||
int val = ui->spinBoy->value();
|
||||
ui->spinBoy->setValue(++val);
|
||||
}
|
||||
void Widget::on_btnGirlInc_clicked()
|
||||
{
|
||||
int val = ui->spinGirl->value();
|
||||
ui->spinGirl->setValue(++val);
|
||||
}
|
||||
void Widget::on_btnClassInfo_clicked()
|
||||
{
|
||||
const QMetaObject* meta=boy->metaObject();
|
||||
ui->textEdit->clear();
|
||||
ui->textEdit->append("==元对象信息==\n");
|
||||
ui->textEdit->append(
|
||||
QString("类名: %1\n").arg(meta->className()));
|
||||
ui->textEdit->append("properties:\n");
|
||||
for (int i= meta->propertyOffset(); i < meta->propertyCount(); ++i)
|
||||
{
|
||||
QMetaProperty prop = meta->property(i);
|
||||
auto propName = prop.name();
|
||||
auto propVal = boy->property(propName).toString();
|
||||
ui->textEdit->append(
|
||||
QString("属性名=%1,属性值=%2").arg(propName).arg(propVal));
|
||||
}
|
||||
ui->textEdit->append("");
|
||||
ui->textEdit->append("classInfo");
|
||||
for (int i= meta->classInfoOffset(); i< meta->classInfoCount(); ++i)
|
||||
{
|
||||
QMetaClassInfo classInfo = meta->classInfo(i);
|
||||
ui->textEdit->append(QString("Name=%1; Value=%2").arg(classInfo.name()).arg(classInfo.value()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user