当前位置:网站首页>Qt(五)元对象特性
Qt(五)元对象特性
2022-07-15 18:00:00 【51CTO】
文章目录
一、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
>
- 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.
二、代码开发
新建类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(
"小明");
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()));
}
}
- 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
三、演示

边栏推荐
- 华泰证券开户安全吗?怎么开户?
- CTSI foundation reserve transparency report - July 2022
- One question per day · 735 Planetary collision · stack simulation
- Log in and operate the database with the command line
- Qt(二)UI控件简介与 可选树状控件演示
- Unity iotar augmented reality tutorial
- The space dream of billionaires is killing the earth
- Create and generate WiFi QR code mobile phone scanning link
- R language uses the melt function of reshape2 package to transform dataframe data from wide table to long table, and dcast function processes the data processed by melt function from long table to wid
- umask计算创建文件、目录的默认权限
猜你喜欢

Qt(二)UI控件简介与 可选树状控件演示

Unity IoTAR物联网增强现实教程

Software architecture and design (x) -- Architecture Technology

Matlab:数据集读取 imageDatastore() 的用法

338. Bit counting · dynamic programming

Des dizaines de milliards de données compressées à 600 go, tdengine est installé sur la plateforme mobile d'énergie de GCL

Li Mu hands on deep learning V2 anchor box and code implementation in target detection

What's the use of fftshift? Why does matlab need fftshift after FFT?

安徽大学店铺

mysql 报错 mysqld:sort aborted:Server shutdown in progress 原因
随机推荐
数百亿数据压缩至 600GB,TDengine 落地协鑫能科移动能源平台
307. Area and retrieval - array modifiable
Torch in pytoch Sort() and torch Argsort() function parsing
LDAP introduction
A-F codeworks round 806 (div.4) A-F problem solution and code
406·根据身高重建队列·贪心
Consumer start flash back
Le langage r utilise la fonction Melt du paquet reshape 2 pour transformer les données de la table large en la table longue, la fonction dcast traite les données de la fonction Melt, et la table longu
Is CICC wealth securities a formal securities company? Is it safe to open an account
@Repository @ [email protected] Understanding of annotations
The space dream of billionaires is killing the earth
Race · 6116 · calculate the value of Boolean binary tree · recursion
Matlab: usage of reading imagedatastore() from dataset
使用POI替换word中的特定字符/文字改进版
蓝鲸配置框架
If in SQL determines the error
Meituan side: why does thread crash not cause JVM crash?
How do I open an account with tongdaxin? Is it safe to open a stock account by mobile phone?
Usage analysis of [:, none] in pytoch
leetcode 605. Can place flowers planting problem (simple)