当前位置:网站首页>QT (five) meta object properties
QT (five) meta object properties
2022-07-27 15:33:00 【51CTO】
List of articles
One 、UI Design
New projects , The main options are shown below : One QWidget, belt ui file , And preliminary Ui Design
Drag components :QLable,QPushButton,QSpinBox,QPlainTextEdit.

details ui file
<?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
> Set the boy's age
</
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
> Class meta object information
</
string
>
</
property
>
</
widget
>
</
item
>
<
item
row=
"1"
column=
"0"
>
<
widget
class=
"QLabel"
name=
"label_2"
>
<
property
name=
"text"
>
<
string
> Set the age of the girl
</
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
> Clear dialog
</
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
>
- 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.
Two 、 Code development
The new class 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
- 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.
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));
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
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
- 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.
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(
" Xiao Ming ");
boy
->
setProperty(
"score",
95);
boy
->
setProperty(
"age",
10);
boy
->
setProperty(
"sex",
"Boy");
connect(
boy,
&
QPerson::ageChanged,
this,
&
Widget::on_ageChanged);
girl
=
new
QPerson(
" floret ");
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(
" Age =%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(
"=== Meta object information ====\n");
ui
->
txtEdit
->
appendPlainText(
QString(
" Class name :%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(
" The attribute name :%1、 Property value %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()));
}
}
- 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.
main.cpp
3、 ... and 、 demonstration

边栏推荐
- JMeter recording interface automation
- Leetcode 456.132 mode monotone stack /medium
- DevEco Studio2.1运行项目报错
- Leetcode 240. search two-dimensional matrix II medium
- JUC(JMM、Volatile)
- With just two modifications, apple gave styleganv2 3D generation capabilities
- How to edit a framework resource file separately
- STM32F10x_ Hardware I2C read / write EEPROM (standard peripheral library version)
- 魔塔项目中的问题解决
- MySQL interview 40 consecutive questions, interviewer, if you continue to ask, I will turn my face
猜你喜欢
Comparison of advantages and disadvantages between instrument amplifier and operational amplifier

DIY ultra detailed tutorial on making oscilloscope: (1) I'm not trying to make an oscilloscope
MOS管防止电源反接的原理

CAN总线的EMC设计方案

generic paradigm

IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一

EMC design scheme of CAN bus

Unity性能优化------渲染优化(GPU)之LOD(Level of detail)

Code coverage statistical artifact -jacobo tool practice
Principle of MOS tube to prevent reverse connection of power supply
随机推荐
修改frameworks资源文件如何单编
After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues
魔塔项目中的问题解决
4种单片机驱动继电器方案
泛型
/dev/loop1占用100%问题
Digital storage oscilloscope based on FIFO idt7202-12
Notice of Shenzhen Municipal Bureau of human resources and social security on the issuance of employment related subsidies for people out of poverty
Introduction of the connecting circuit between ad7606 and stm32
STM32 can -- can ID filter analysis
Spark Filter算子在Parquet文件上的下推
适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制
ADB command (install APK package format: ADB install APK address package name on the computer)
Four kinds of relay schemes driven by single chip microcomputer
Leetcode 244周赛-赛后补题题解【西兰花选手】
Network equipment hard core technology insider router Chapter 10 Cisco asr9900 disassembly (III)
ad7606与stm32连接电路介绍
shell脚本读取文本中的redis命令批量插入redis
Zhou Hongyi: if the digital security ability is backward, it will also be beaten
generic paradigm