Qt 由字符串创建对象

由 Animial 基类派生出 Dog 类 和 Cat 类,分别实现自己的 voice() 方法;注册 Dog类以及 Cat 类,然后由字符串 Dog 和 Cat 创建对象。

参考文章:

  • Object factory
    https://www.mimec.org/blog/object-factory
  • Qt通过类名称创建类的实例的两种方法
    https://blog.csdn.net/zgrjkflmkyc/article/details/84837541
  • Qt中如何根据类名来实例化对象
    https://www.cnblogs.com/liushui-sky/p/6559973.html
  • QT根据类名动态创建类对象-CSDN论坛
    https://bbs.csdn.net/topics/340251573
  • Qt中如何根据类名来实例化对象-CSDN论坛
    https://bbs.csdn.net/topics/390480286/
  • Qt根据类名创建对象(元对象反射)
    https://www.cnblogs.com/ztzheng/p/4122975.html
  • Qt元对象(Meta-Object)系统与反射 – C++ Programer
    http://www.cryfeifei.cn/395.html
  • c++反射—-现有框架
    https://zhuanlan.zhihu.com/p/99223617
  • Qt类反射机制
    https://blog.csdn.net/kenier/article/details/62041312
#ifndef ANIMAL_H
#define ANIMAL_H

#include <QObject>

class Animal : public QObject
{
    Q_OBJECT
public:
    explicit Animal(QObject *parent = nullptr);
    virtual void voice();

signals:

public slots:
};

#endif // ANIMAL_H
#include "animal.h"

Animal::Animal(QObject *parent) : QObject(parent)
{

}

void Animal::voice()
{

}
#ifndef CAT_H
#define CAT_H
#include "animal.h"


class Cat : public Animal
{
    Q_OBJECT

public:
    Cat(QObject *parent = nullptr);
    void voice();
};

#endif // CAT_H
#include "cat.h"
#include <QDebug>

Cat::Cat(QObject *parent)
{

}

void Cat::voice()
{
    qDebug() <<"miao~";
}
#ifndef DOG_H
#define DOG_H
#include "animal.h"


class Dog : public Animal
{
    Q_OBJECT

public:
    Dog(QObject *parent = nullptr);
    void voice();
};

#endif // DOG_H
#include "dog.h"
#include <QDebug>

Dog::Dog(QObject *parent)
{

}

void Dog::voice()
{
    qDebug() << "wang!";

}


#include <QByteArray>
#include <QMetaObject>
#include <QHash>


#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 = nullptr )
    {
        Constructor constructor = constructors().value( className );
        if ( constructor == nullptr )
            return nullptr;
        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
#include <QCoreApplication>
#include "dog.h"
#include "cat.h"
#include <QDebug>
#include <QMap>
#include "objectfactory.h"


int main( )
{
    ObjectFactory::registerClass<Dog>();
    ObjectFactory::registerClass<Cat>();

    Animal* animal = qobject_cast<Animal*>(ObjectFactory::createObject( "Dog" ));
    animal->voice();

    animal = qobject_cast<Animal*>(ObjectFactory::createObject( "Cat" ));
    animal->voice();

    return 0;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.