网络上下载的类:
#ifndef OBJECTFACTORY_H
#define OBJECTFACTORY_H
class ObjectFactory
{
public:
template<typename T>
static void registerClass()
{
constructors().insert( T::staticMetaObject.className(), &constructorHelper<T> );
}
static QObject* createObject( const QByteArray& className, QObject* parent = NULL )
{
Constructor constructor = constructors().value( className );
if ( constructor == NULL )
return NULL;
return (*constructor)( parent );
}
private:
typedef QObject* (*Constructor)( QObject* parent );
template<typename T>
static QObject* constructorHelper( QObject* parent )
{
return new T( parent );
}
static QHash<QByteArray, Constructor>& constructors()
{
static QHash<QByteArray, Constructor> instance;
return instance;
}
};
#endif // OBJECTFACTORY_H
自己建立的测试类:
#ifndef CDXOBJECT_H
#define CDXOBJECT_H
#include <QObject>
#include "QString"
class CdxObject : public QObject
{
Q_OBJECT
public:
QString m_sName;
public:
explicit CdxObject(QObject *parent = 0);
signals:
public slots:
};
class CdxDrawObject : public CdxObject
{
Q_OBJECT
public:
explicit CdxDrawObject(QObject* parent);
signals:
public slots:
};
class CdxNetworkObject : public CdxObject
{
Q_OBJECT
public:
explicit CdxNetworkObject(QObject* parent);
signals:
public slots:
};
#endif // CDXOBJECT_H
//cdxobject.cpp
#include "cdxobject.h"
CdxObject::CdxObject(QObject *parent) : QObject(parent)
{
m_sName="root object";
}
CdxDrawObject::CdxDrawObject(QObject* parent)
{
m_sName="draw object";
}
CdxNetworkObject::CdxNetworkObject(QObject* parent)
{
m_sName="network object";
}
//调用测试
#include "cdxobject.h"
#include "objectfactory.h"
void MainWindow::on_pushButton_clicked()
{
ObjectFactory::registerClass<CdxObject>();
ObjectFactory::registerClass<CdxDrawObject>();
ObjectFactory::registerClass<CdxNetworkObject>();
//CdxObject* object = (CdxObject*)ObjectFactory::createObject( "CdxObject" );
//CdxObject* object = (CdxObject*)ObjectFactory::createObject( "CdxDrawObject" );
CdxObject* object = (CdxObject*)ObjectFactory::createObject( "CdxNetworkObject" );
ui->lineEdit->setText(object->m_sName);
}
#ifndef OBJECTFACTORY_H
#define OBJECTFACTORY_H
class ObjectFactory
{
public:
template<typename T>
static void registerClass()
{
constructors().insert( T::staticMetaObject.className(), &constructorHelper<T> );
}
static QObject* createObject( const QByteArray& className, QObject* parent = NULL )
{
Constructor constructor = constructors().value( className );
if ( constructor == NULL )
return NULL;
return (*constructor)( parent );
}
private:
typedef QObject* (*Constructor)( QObject* parent );
template<typename T>
static QObject* constructorHelper( QObject* parent )
{
return new T( parent );
}
static QHash<QByteArray, Constructor>& constructors()
{
static QHash<QByteArray, Constructor> instance;
return instance;
}
};
#endif // OBJECTFACTORY_H
自己建立的测试类:
#ifndef CDXOBJECT_H
#define CDXOBJECT_H
#include <QObject>
#include "QString"
class CdxObject : public QObject
{
Q_OBJECT
public:
QString m_sName;
public:
explicit CdxObject(QObject *parent = 0);
signals:
public slots:
};
class CdxDrawObject : public CdxObject
{
Q_OBJECT
public:
explicit CdxDrawObject(QObject* parent);
signals:
public slots:
};
class CdxNetworkObject : public CdxObject
{
Q_OBJECT
public:
explicit CdxNetworkObject(QObject* parent);
signals:
public slots:
};
#endif // CDXOBJECT_H
//cdxobject.cpp
#include "cdxobject.h"
CdxObject::CdxObject(QObject *parent) : QObject(parent)
{
m_sName="root object";
}
CdxDrawObject::CdxDrawObject(QObject* parent)
{
m_sName="draw object";
}
CdxNetworkObject::CdxNetworkObject(QObject* parent)
{
m_sName="network object";
}
//调用测试
#include "cdxobject.h"
#include "objectfactory.h"
void MainWindow::on_pushButton_clicked()
{
ObjectFactory::registerClass<CdxObject>();
ObjectFactory::registerClass<CdxDrawObject>();
ObjectFactory::registerClass<CdxNetworkObject>();
//CdxObject* object = (CdxObject*)ObjectFactory::createObject( "CdxObject" );
//CdxObject* object = (CdxObject*)ObjectFactory::createObject( "CdxDrawObject" );
CdxObject* object = (CdxObject*)ObjectFactory::createObject( "CdxNetworkObject" );
ui->lineEdit->setText(object->m_sName);
}