Ok. I think I have done it.
#include <QtCore/QCoreApplication>
#include "test.h"
int main(int argc, char *argv[])
{
holder *h = qvariant_cast<holder*>(v);
return a.exec();
}
#include <QtCore/QCoreApplication>
#include "test.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QVariant v = QVariant::fromValue(new holder(new impl()));
holder *h = qvariant_cast<holder*>(v);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
#ifndef TEST_H
#define TEST_H
#include <QtCore>
class intf
{
public:
virtual void xxx() = 0;
};
class impl : public intf
{
int x;
public:
impl()
{
x = 5;
}
void xxx()
{
}
};
typedef struct holder
{
intf* ptr;
char* xx;
holder()
{
ptr = NULL;
xx = new char[10];
strcpy(xx, "lksjd");
}
holder(intf* p)
{
ptr = p;
xx = new char[10];
strcpy(xx, "lksjd");
}
};
Q_DECLARE_METATYPE(holder)
Q_DECLARE_METATYPE(holder*)
#endif
#ifndef TEST_H
#define TEST_H
#include <QtCore>
class intf
{
public:
virtual void xxx() = 0;
};
class impl : public intf
{
int x;
public:
impl()
{
x = 5;
}
void xxx()
{
}
};
typedef struct holder
{
intf* ptr;
char* xx;
holder()
{
ptr = NULL;
xx = new char[10];
strcpy(xx, "lksjd");
}
holder(intf* p)
{
ptr = p;
xx = new char[10];
strcpy(xx, "lksjd");
}
};
Q_DECLARE_METATYPE(holder)
Q_DECLARE_METATYPE(holder*)
#endif
To copy to clipboard, switch view to plain text mode
After performing the cast in main, I get the same structure, which is good .
Those class members are there just to assure I'm getting the same thing back from the qvariant.
Please note:
Q_DECLARE_METATYPE(holder)
Q_DECLARE_METATYPE(holder*)
Q_DECLARE_METATYPE(holder)
Q_DECLARE_METATYPE(holder*)
To copy to clipboard, switch view to plain text mode
Also holder*( has to be registered, otherwise it won't work. I forgot to mention this earlier.
I thing you too can use this.(sorry for the messy code).
Instead of an "impl" instance you can pass the pointer to your interface when building the qvariant.
Regards
Bookmarks