Skip to content

Instantly share code, notes, and snippets.

#include <QObject>
#include <QDebug>
class FactoryBase
{
public:
virtual QObject* create() = 0;
};
QMap<QString, FactoryBase*> factoryMap;
@SunRain
SunRain / gist:f1c8481b669655bc2e85
Created October 16, 2014 15:17
Qt-creating-class-dynamically-from-its
http://kunalmaemo.blogspot.com/2010/07/creating-class-dynamically-from-its.html
Some time it could be useful to be able to create new instance of class just by knowing its name. Such as in situation like when you are receiving XML from network and you want to create certain type of parser depending upon XML received from network.
In Qt its possible by using Qt's meta object system.
Here is my sample implementation that uses QMetaType to achieve the same. In sample code Parser is base class of all parser.
class Parser
@SunRain
SunRain / gist:7174eaa2f8f81202d919
Last active August 29, 2015 14:07
QT根据类名动态创建类对象
http://bbs.csdn.net/topics/340251573?page=2
http://bbs.csdn.net/topics/390480286
#include <map>
#include <string>
#include <iostream>
namespace MyNameSpace {
class Base
{
public:
virtual char* GetName() const = 0;
};