当前位置:网站首页>QT document reading notes staticmetaobject parsing and instances
QT document reading notes staticmetaobject parsing and instances
2022-06-21 23:30:00 【IT1995】
Official analysis
const QMetaObject *QObject::metaObject() const
Meta objects contain inheritance QObject Class ,QObject Subclasses of , Class name , Subclass name , attribute , Signals, slots, etc .
have access to metaObject The instance specifies the object , The following code :
QObject *obj = new QPushButton;
obj->metaObject()->className(); // returns "QPushButton"
QPushButton::staticMetaObject.className(); // returns "QPushButton"Blogger examples
Here's a definition TestClass, Use Qt The reflex mechanism , Create a class , And set the value to print
Source code is as follows :
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();
// get attribute
QStringList propertyList;
for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); i++){
propertyList << metaObject->property(i).name();
}
// Set value by reflection
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;
}The screenshot of the program is as follows :

Source code package download address :
边栏推荐
- 逆向调试入门-基础知识
- C delegate
- Native applet application applet - publishing process
- 软件测试 答疑篇
- Shanghai Jiaotong University: Kerong Wang | bootstrap transformer based on off-line reinforcement learning
- MVN deploy bat files for multiple modules
- Readjustment of move protocol beta to expand the total prize pool
- An example of data format conversion in MySQL
- [understanding pointer] advanced level of pointer
- postgis 如何用米制单位 buffer
猜你喜欢

Cola and herbal tea speed up mutual rolling

阿里出品!图形化的ant脚本——IDEA插件CloudToolkit

关于 麒麟系统开发错误“fatal error: GL/gl.h: No such file or directory“ 的解决方法

4. esp8266 displays DHT11 temperature and humidity parameters in real time through OLED

Analysis of 43 cases of MATLAB neural network: Chapter 9 associative memory of discrete Hopfield Neural Network -- number recognition

If you spend 200W to buy traffic, it is better to start at 0 cost and make an independent station with high private domain operation income!

leetcode1337. 矩阵中战斗力最弱的K行

麒麟系统开发笔记(五):制作安装麒麟系统的启动U盘、物理机安装麒麟系统以及搭建Qt开发环境

Solution to garbled Chinese display of securefx transmission remote server

CISSP certification 2021 textbook OSG 9th Edition added (modified) knowledge points: comparison with the 8th Edition
随机推荐
硬件开发笔记(四):硬件开发基本流程,制作一个USB转RS232的模块(三):设计原理图
Uni app advanced style framework / production environment [Day10]
Qt实用技巧:QtCreator编辑区关闭右侧不必要的警告提示
Move Protocol Beta测试版再调整,扩大总奖池
About the solution to the "fatal error: gl/gl.h: no such file or directory" of Qilin system development error
Uniapp encapsulates the request function to achieve unique login. One account can only log in to one device at the same time
RK3568开发笔记(三):RK3568虚拟机基础环境搭建之更新源、安装网络工具、串口调试、网络连接、文件传输、安装vscode和samba共享服务
Analysis of 43 cases of MATLAB neural network: Chapter 9 associative memory of discrete Hopfield Neural Network -- number recognition
UniApp之播放视频、 下载视频到手机相册、添加下载进度条功能(踩坑记录)
Elementary transformation of numpy matrix
Is it safe for the head teacher to open a stock account in qiniu?
你有一个机会,这里有一个舞台
4. esp8266 displays DHT11 temperature and humidity parameters in real time through OLED
Native applet application applet - publishing process
Readjustment of move protocol beta to expand the total prize pool
danfoss丹佛斯变频器维修VLT5000/VLT6000/VLT8000
2022年云计算的趋势有哪些?
Explain JS micro task and macro task in simple terms
[WUSTCTF2020]朴实无华-1
深度学习预测酶活性参数提升酶约束模型构建从头环境搭建
https://github.com/fengfanchen/Qt/tree/master/StaticMetaObjectDemo