Qt动态重生类厂
Qt 动态 重生
2023-09-14 09:07:11 时间
class QConstructHelper
{
public:
virtual void * create() = 0;
};
template<typename T>
class QMetaTypeConstructHelper : public QConstructHelper
{
public:
QMetaTypeConstructHelper(){}
void * create(){return new T;}
};
template<typename T>
int qRegisterConstructHelper(const char *name)
{
return qRegisterMetaType< QMetaTypeConstructHelper<T> >(name);
}
void * qConstructHelper(const char* name);
QObject * qObjectConstructHelper( const char* name );
//关键函数的实现
void * qConstructHelper(const char* name)
{
int id = QMetaType::type( name );
if (id != QMetaType::UnknownType)
{
QConstructHelper * creator = static_cast<QConstructHelper*>(QMetaType::create( id ));
if(creator)
{
void * p = creator->create();
QMetaType::destroy(id,creator);
return p;
}
}
return NULL;
}
QObject * qObjectConstructHelper( const char* name )
{
return static_cast<QObject*>(qConstructHelper(name));
}
用法示例:
qRegisterConstructHelper<Data>("myData");
qRegisterConstructHelper<Widget>("widget");
Widget * w = qobject_cast<Widget*>(qObjectConstructHelper("widget"));
if(w)
{
w->show();
return a.exec();
}
另外这个函数可以创建Qt的原生对象(除窗体对象已外的)
void * qMetaTypeCreate(const char *name)
{
int id = QMetaType::type( name );
if (id != QMetaType::UnknownType)
{
return QMetaType::create( id );
}
return NULL;
}
相关文章
- Qt-Chart在的使用(生成双线、隐藏线,实时动态生成线、时间轴、定义线的颜色)
- Qt音视频开发20-vlc内核动态保存录像文件(不需要重新编译源码)
- Qt编写项目作品35-数据库综合应用组件
- Qt编写地图综合应用56-实时动态轨迹
- Qt编写地图综合应用25-echart动态交互
- Qt 处理相机图像实时显示引入队列,防止数据读写冲突导致卡顿、崩溃
- Qt OpenGL 2D图像文字
- Qt动态如何添加
- QT 数据库编程
- QT信号槽机制
- Qt多线程间信号槽传递非QObject类型对象的参数
- Qt设置鼠标等待状态和恢复
- qt 中回调函数的实现
- Qt生成和调用动态库dll,和静态库.a(windows和linux通用)
- QT TextEdit设置背景、明文加密、弹出网站、弹出自定义对话框、gif动态图片、程序启动动画、打包程序、关闭事件
- Qt自定义信号和槽
- Qt编写可视化大屏电子看板系统7-窗体浮动