文章目录
一、UI设计
新建项目,主要选项如下图:一个QWidget,带ui文件,并初步Ui设计
拖动组件:QLable,QPushButton,QSpinBox,QPlainTextEdit。
详情ui文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>534</width>
<height>388</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>设置男孩年龄</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="spinBoxBoy">
<property name="value">
<number>10</number>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="pushButtonBoyUp">
<property name="text">
<string>boy_up_age</string>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QPushButton" name="pushButtonMetaInfo">
<property name="text">
<string>类的元对象信息</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>设置女孩年龄</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="spinBoxGirl">
<property name="value">
<number>5</number>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="pushButtonGirlUp">
<property name="text">
<string>girl_up_age</string>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QPushButton" name="pushButtonClear">
<property name="text">
<string>清空对话框</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QPlainTextEdit" name="txtEdit"/>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>pushButtonClear</sender>
<signal>clicked()</signal>
<receiver>txtEdit</receiver>
<slot>clear()</slot>
<hints>
<hint type="sourcelabel">
<x>461</x>
<y>57</y>
</hint>
<hint type="destinationlabel">
<x>266</x>
<y>228</y>
</hint>
</hints>
</connection>
</connections>
</ui>
二、代码开发
新建类QPerson
qperson.h
#ifndef QPERSON_H
#define QPERSON_H
#include <QObject>
class QPerson : public QObject
{
Q_OBJECT
Q_CLASSINFO("author","wsp_1138886114")
Q_CLASSINFO("company","SH")
Q_CLASSINFO("version","1.0")
Q_PROPERTY(unsigned age READ age WRITE setAge NOTIFY ageChanged)
Q_PROPERTY(QString name MEMBER m_name)
Q_PROPERTY(int score MEMBER m_score)
private:
unsigned m_age = 10;
QString m_name;
int m_score = 79;
public:
explicit QPerson(QString name, QObject *parent = nullptr);
unsigned age();
void setAge(unsigned value);
void ageInc();
signals:
void ageChanged(unsigned value);
};
#endif // QPERSON_H
qperson.cpp
#include "qperson.h"
QPerson::QPerson(QString name, QObject *parent) : QObject(parent)
{
m_name = name;
}
unsigned QPerson::age()
{
return m_age;
}
void QPerson::setAge(unsigned value)
{
m_age = value;
emit(ageChanged(m_age));
}
void QPerson::ageInc()
{
m_age++;
emit(ageChanged(m_age));
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "qperson.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
private:
QPerson* boy;
QPerson* girl;
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
void on_ageChanged(unsigned value);
private slots:
void on_pushButtonBoyUp_clicked();
void on_pushButtonGirlUp_clicked();
void on_pushButtonMetaInfo_clicked();
void on_spin_valueChanged(int arg1);
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QMetaProperty>
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",95);
boy->setProperty("age",10);
boy->setProperty("sex","Boy");
connect(boy,&QPerson::ageChanged,this,&Widget::on_ageChanged);
girl = new QPerson("小花");
girl->setProperty("score",99);
girl->setProperty("age",9);
girl->setProperty("sex","girl");
connect(girl,&QPerson::ageChanged,this,&Widget::on_ageChanged);
connect(ui->spinBoxBoy,SIGNAL(valueChanged(int)),this,SLOT(on_spin_valueChanged(int)));
connect(ui->spinBoxGirl,SIGNAL(valueChanged(int)),this,SLOT(on_spin_valueChanged(int)));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_ageChanged(unsigned value)
{
Q_UNUSED(value)
QPerson* aPerson = qobject_cast<QPerson*>(sender());
QString aName = aPerson->property("name").toString();
QString aSex = aPerson->property("sex").toString();
unsigned aAge = aPerson->age();
ui->txtEdit->appendPlainText(aName+","+aSex+QString::asprintf(" 年龄=%d",aAge));
}
void Widget::on_spin_valueChanged(int arg1)
{
Q_UNUSED(arg1)
QSpinBox* spinBox = qobject_cast<QSpinBox*>(sender());
if (spinBox->property("isBoy").toBool())
boy->setAge(spinBox->value());
else
girl->setAge(spinBox->value());
}
void Widget::on_pushButtonBoyUp_clicked()
{
boy->ageInc();
}
void Widget::on_pushButtonGirlUp_clicked()
{
girl->ageInc();
}
void Widget::on_pushButtonMetaInfo_clicked()
{
const QMetaObject* meta = girl->metaObject();
ui->txtEdit->clear();
ui->txtEdit->appendPlainText("===元对象信息====\n");
ui->txtEdit->appendPlainText(
QString("类名称:%1\n").arg(meta->className()));
ui->txtEdit->appendPlainText("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("");
ui->txtEdit->appendPlainText("classInfo");
for (int i=meta->classInfoOffset();i<meta->classInfoCount();i++) {
QMetaClassInfo classinfo = meta->classInfo(i);
ui->txtEdit->appendPlainText(QString("name=%1, value=%2").arg(classinfo.name()).arg(classinfo.value()));
}
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
三、演示