#include#include // Create a custom data type class MyData { public: MyData(int a, QString b) : m_a(a), m_b(b) {} int a() const { return m_a; } QString b() const { return m_b; } private: int m_a; QString m_b; }; // Register the custom data type Q_DECLARE_METATYPE(MyData) int main() { // Create an instance of the custom data type MyData mydata(42, "Hello world"); // Store the data in a QVariant QVariant var; var.setValue(mydata); // Retrieve the data from the QVariant MyData data = var.value (); qDebug() << data.a() << data.b(); // Output: 42 Hello world }
#includeThese examples are part of the Qt framework.#include class MyClass : public QObject { Q_OBJECT public slots: void setData(QVariant var) { MyData data = var.value (); qDebug() << data.a() << data.b(); // Output: 42 Hello world } }; int main() { // Create an instance of the custom data type MyData mydata(42, "Hello world"); // Register the custom data type qRegisterMetaType (); // Connect a signal to a slot MyClass obj; QObject::connect(&myObj, SIGNAL(sendData(QVariant)), &obj, SLOT(setData(QVariant))); // Emit the signal QVariant var; var.setValue(mydata); emit sendData(var); }