[Interest] How to create QObject-derived class instance from class name

Prashanth Udupa prashanth.udupa at gmail.com
Tue Mar 1 17:36:12 CET 2016


But we can do this though

--
#include <QMetaMethod>
#include <QObject>
#include <QtDebug>

class Object1 : public QObject
{
    Q_OBJECT

public:
    Q_INVOKABLE Object1(QObject *parent=0) : QObject(parent) { }
    ~Object1() { }
};

class Object2 : public QObject
{
    Q_OBJECT

public:
    Q_INVOKABLE Object2(QObject *parent=0) : QObject(parent) { }
    ~Object2() { }
};

class Object3 : public QObject
{
    Q_OBJECT

public:
    Q_INVOKABLE Object3(QObject *parent=0) : QObject(parent) { }
    ~Object3() { }
};

QObject *createInstance(const QByteArray &className, QObject *parent=0)
{
    const QByteArray className2 = className + "*";
    const int type = QMetaType::type( className2 );
    if(type == QMetaType::UnknownType)
        return 0;

    const QMetaObject *mo = QMetaType::metaObjectForType(type);
    if(!mo)
        return 0;

    QObject *objectPtr = mo->newInstance(Q_ARG(QObject*,parent));
    return objectPtr;
}

int main()
{
    qRegisterMetaType<Object1*>("Object1*");
    qRegisterMetaType<Object2*>("Object2*");
    qRegisterMetaType<Object3*>("Object3*");

    const QList<QByteArray> classNames = QList<QByteArray>()
            << "Object1" << "Object2" << "Object3";

    QObject parentObj;

    Q_FOREACH(QByteArray className, classNames)
    {
        QObject *o = ::createInstance(className, &parentObj);
        if(o)
            qDebug() << className << ": Created " <<
o->metaObject()->className();
        else
            qDebug() << className << ": Could not create instance";
    }

    return 0;
}

#include "main.moc"
--

Works for me.

/ Prashanth

On Tue, 1 Mar 2016 at 21:52 Thiago Macieira <thiago.macieira at intel.com>
wrote:

> On terça-feira, 1 de março de 2016 17:06:49 PST André Somers wrote:
> > > The meta *object* system has no registration.
> > >
> > > The meta *type* system requires that the registered type be default-
> > > constructible and copyable, but QObject is not copyable. Therefore,
> > > QObject- derived classes cannot be registered with the meta type
> system.
> >
> > Am I completely misinterpretting the documentation then?
> > http://doc.qt.io/qt-5/qmetatype.html#metaObject
> >
> > If I read that correctly, you can register a
> > pointer-to-a-QObject-derived-class-instance and use that. So, indeed,
> > you do not register the type, but the type*. And that has no problems
> > with being default constructed or copied.
>
> Correct. You can't register a QObject class with the meta type system, but
> you
> can register a pointer to a QObject class. The problem is that
> QMetaType::create() will then create a pointer, not the object.
>
> --
> Thiago Macieira - thiago.macieira (AT) intel.com
>   Software Architect - Intel Open Source Technology Center
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160301/13a45bf5/attachment.html>


More information about the Interest mailing list