当前位置:网站首页>Qt文档阅读笔记-staticMetaObject解析与实例
Qt文档阅读笔记-staticMetaObject解析与实例
2022-06-21 21:39:00 【IT1995】
官方解析
const QMetaObject *QObject::metaObject() const
元对象包含继承QObject的类,QObject的子类,类名,子类名,属性,信号和槽等等等。
可以使用metaObject实例会指定对象,如下代码:
QObject *obj = new QPushButton;
obj->metaObject()->className(); // returns "QPushButton"
QPushButton::staticMetaObject.className(); // returns "QPushButton"博主例子
这里定义了一个TestClass,使用Qt反射机制,创建类,并设置值打印
源码如下:
TestClass.h
#ifndef TESTCLASS_H
#define TESTCLASS_H
#include <QObject>
#include <QDebug>
class TestClass : public QObject
{
Q_OBJECT
Q_PROPERTY(int intValue READ getIntValue WRITE setIntValue)
Q_PROPERTY(float floatValue READ getFloatValue WRITE setFloatValue)
Q_PROPERTY(QString qstringValue READ getQStringValue WRITE setQStringValue)
Q_PROPERTY(qlonglong qlonglongValue READ getQlonglongValue WRITE setQlonglongValue)
Q_PROPERTY(QList<QVariant> qListValue READ getQListValue WRITE setQListValue)
public:
Q_INVOKABLE TestClass(){}
~TestClass(){ qDebug() << "~TestClass() called"; }
friend QDebug operator << (QDebug os, TestClass &testClass){
os << "[";
os << "(" << "intValue: " << testClass.getIntValue() << ", " <<
"floatValue: " << testClass.getFloatValue() << ", " <<
"QStringValue: " << testClass.getQStringValue() << ", " <<
"qlonglongValue: " << testClass.getQlonglongValue() << ")";
for(const QVariant &item : testClass.getQListValue()){
os << "list: " << item;
}
os << "]";
return os;
}
int getIntValue(){
return this->intValue;
}
float getFloatValue(){
return this->floatValue;
}
QString getQStringValue(){
return this->qstringValue;
}
qlonglong getQlonglongValue(){
return this->qlonglongValue;
}
QList<QVariant> getQListValue(){
return this->qListValue;
}
void setIntValue(int value){
this->intValue = value;
}
void setFloatValue(float value){
this->floatValue = value;
}
void setQStringValue(QString value){
this->qstringValue = value;
}
void setQlonglongValue(qlonglong value){
this->qlonglongValue = value;
}
void setQListValue(QList<QVariant> value){
qListValue.clear();
for(const QVariant &item : value){
this->qListValue.append(item);
}
}
private:
int intValue;
float floatValue;
QString qstringValue;
qlonglong qlonglongValue;
QList<QVariant> qListValue;
};
#endif // TESTCLASS_Hmain.cpp
#include "TestClass.h"
#include <QMetaObject>
#include <QMetaProperty>
int main(int argc, char *argv[])
{
Q_UNUSED(argc)
Q_UNUSED(argv)
const QMetaObject *metaObject = &TestClass::staticMetaObject;
QObject *obj = metaObject->newInstance();
//获取属性
QStringList propertyList;
for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); i++){
propertyList << metaObject->property(i).name();
}
//通过反射设置值
for(const QString &property : propertyList){
if(property == "intValue"){
obj->setProperty(property.toUtf8(), 10);
}
else if(property == "floatValue"){
obj->setProperty(property.toUtf8(), 10.1f);
}
else if(property == "qstringValue"){
obj->setProperty(property.toUtf8(), "HelloWorld");
}
else if(property == "qlonglongValue"){
obj->setProperty(property.toUtf8(), 9999999999999);
}
else if(property == "qListValue"){
QList<QVariant> list;
list << "10086" << "10010" << "10000";
obj->setProperty(property.toUtf8(), list);
}
}
qDebug() << (*(TestClass*)obj);
delete obj;
return 0;
}程序运行截图如下:

源码打包下载地址:
边栏推荐
- What is the most challenging issue in Bi development?
- matplotlib画图显示中文
- uniapp版本更新 热更新及自然更新
- jupyter-lab安装扩展后无法使用
- Pltmodify abscissa and ordinate colors
- uni-app进阶之样式框架/生产环境【day10】
- Mongo uses too much memory and is automatically shut down by the system
- 378. the k-th smallest element in an ordered matrix - conventional method
- 关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法
- 有一说一,高并发系统设计其实一点都不难!
猜你喜欢

Flow cytometry flowjo 10 introduction and ultra detailed graphic installation activation tutorial

Flink实战之实时风控规则引擎

硬件开发笔记(四):硬件开发基本流程,制作一个USB转RS232的模块(三):设计原理图

Project process management tool OmniPlan Pro 4

Move Protocol Beta测试版再调整,扩大总奖池

上海交通大学:Kerong Wang | 基于离线强化学习的自举Transformer

About LG (n!) Asymptotically compact supremum of

uniapp在解决谷歌浏览器跨域问题,在谷歌浏览器运行

Danfoss inverter maintenance vlt5000/vlt6000/vlt8000
![[highly recommended] markdown grammar](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[highly recommended] markdown grammar
随机推荐
numpy矩阵初等变换
Resolve the invalidation of OpenCV code prompt in pycharm
Record the abnormal task status caused by an MQ concurrent consumption
4. esp8266 displays DHT11 temperature and humidity parameters in real time through OLED
[WUSTCTF2020]朴实无华-1
Matplotlib drawing and displaying Chinese
postgres 通过 limit 切片数据
Analysis of 43 cases of MATLAB neural network: Chapter 9 associative memory of discrete Hopfield Neural Network -- number recognition
Migration of stm32f407 program to stm32f429
樹莓派開發筆記(十六):樹莓派4B+安裝mariadb數據庫(mysql開源分支)並測試基本操作
Flink实战之实时风控规则引擎
Flink real-time risk control rule engine
Distributed database uses logical volume to manage storage expansion
Produced by Ali! Graphical ant script - idea plug-in cloudtoolkit
Uniapp solves the cross domain problem of Google browser and runs in Google browser
一家低代码厂商停服后……
Résolution de l'erreur de développement du système Kirin "erreur réelle: gl / gl.h: pas de fichier ou de répertoire"
C语言【隐式类型转换】和【显式类型转换】的详解
1016. 子串能表示从 1 到 N 数字的二进制串
Parallel search exercise 1: circle of friends
https://github.com/fengfanchen/Qt/tree/master/StaticMetaObjectDemo