开发者

Qt programming: How to use custom data type in QVariantMap?

开发者 https://www.devze.com 2023-01-27 15:48 出处:网络
I am writing a Qt app that maps a C++ class to Javascript object in QtWebkit. Firstly let me explain what I am trying to do:

I am writing a Qt app that maps a C++ class to Javascript object in QtWebkit. Firstly let me explain what I am trying to do:

I have a class inherited fr开发者_StackOverflowom QObject:

class myobj : public QObject {
    Q_OBJECT
public:
    myobj();
    ~myobj();

pulbic slots:
    void getData();
}

And in another class I tried to add myobj instances to QVariantMap:

QVariantMap anotherClass::getObj() {
    myobj* obj1 = new myobj();
    myobj* obj2 = new myobj();

    QVariantMap items;

    items.insert(QString("0"), QVariant(*obj1));
    items.insert(QString("1"), QVariant(*obj2));

    return items;
}

And then I got the following error:

error: no matching function for call to ‘QVariant::QVariant(myobj&)’

So I tried to add declarations:

Q_DECLARE_METATYPE(myobj);

But I got:

error: ‘QObject::QObject(const QObject&)’ is private

Any idea about this?


Like the compiler said, no constructor of QVariant exists that take a myobj as parameter. Have you tried to use the qVariantFromValue function instead?

I think this is what you are searching for.


If you register your custom type with Q_DECLARE_METATYPE(myobj), your class needs a public default constuctor (ok), a public destructor (ok) and a public copy constructor (MISSING which the error message is telling you), see the documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消