struct MyStruct { int i; ... }; Q_DECLARE_METATYPE(MyStruct)
qRegisterMetaType<MyClass>("MyClass");
int id = QMetaType::type("MyClass"); if (id == 0) { void *myClassPtr = QMetaType::construct(id); ... QMetaType::destroy(id, myClassPtr); myClassPtr = 0; }
C++ (Qt)#ifndef CALCULATOR_H#define CALCULATOR_H #include <QtGui/QMainWindow>#include "GeneratedFiles/ui_calculator.h" #include <QMetaType> class Calculator : public QMainWindow{ Q_OBJECT public: Calculator(QWidget *parent = 0, Qt::WFlags flags = 0); ~Calculator(); private: Ui::CalculatorClass ui;}; Q_DECLARE_METATYPE(Calculator) #endif // CALCULATOR_H
1>------ Build started: Project: Calculator, Configuration: Debug Win32 ------1>Moc'ing calculator.h...1>Compiling...1>calculator.cpp1>c:\base_window\calculator\calculator.h(18) : error C2248: 'QMainWindow::QMainWindow' : cannot access private member declared in class 'QMainWindow'1> c:\qt\4.5.0\include\qtgui\../../src/gui/widgets/qmainwindow.h(206) : see declaration of 'QMainWindow::QMainWindow'1> c:\qt\4.5.0\include\qtgui\../../src/gui/widgets/qmainwindow.h(63) : see declaration of 'QMainWindow'1> This diagnostic occurred in the compiler generated function 'Calculator::Calculator(const Calculator &)'1>moc_calculator.cpp1>c:\base_window\calculator\generatedfiles\debug\../../calculator.h(18) : error C2248: 'QMainWindow::QMainWindow' : cannot access private member declared in class 'QMainWindow'1> c:\qt\4.5.0\include\qtgui\../../src/gui/widgets/qmainwindow.h(206) : see declaration of 'QMainWindow::QMainWindow'1> c:\qt\4.5.0\include\qtgui\../../src/gui/widgets/qmainwindow.h(63) : see declaration of 'QMainWindow'1> This diagnostic occurred in the compiler generated function 'Calculator::Calculator(const Calculator &)'1>main.cpp1>c:\base_window\calculator\calculator.h(18) : error C2248: 'QMainWindow::QMainWindow' : cannot access private member declared in class 'QMainWindow'1> c:\qt\4.5.0\include\qtgui\../../src/gui/widgets/qmainwindow.h(206) : see declaration of 'QMainWindow::QMainWindow'1> c:\qt\4.5.0\include\qtgui\../../src/gui/widgets/qmainwindow.h(63) : see declaration of 'QMainWindow'1> This diagnostic occurred in the compiler generated function 'Calculator::Calculator(const Calculator &)'1>Generating Code...1>Build log was saved at "file://c:\Base_window\Calculator\Debug\BuildLog.htm"1>Calculator - 3 error(s), 0 warning(s)2>------ Build started: Project: Base_window, Configuration: Debug Win32 ------2>Compiling...2>moc_base_window.cpp2>c:\base_window\base_window\../Calculator/calculator.h(18) : error C2248: 'QMainWindow::QMainWindow' : cannot access private member declared in class 'QMainWindow'2> c:\qt\4.5.0\include\qtgui\../../src/gui/widgets/qmainwindow.h(206) : see declaration of 'QMainWindow::QMainWindow'2> c:\qt\4.5.0\include\qtgui\../../src/gui/widgets/qmainwindow.h(63) : see declaration of 'QMainWindow'2> This diagnostic occurred in the compiler generated function 'Calculator::Calculator(const Calculator &)'2>main.cpp2>c:\base_window\base_window\../Calculator/calculator.h(18) : error C2248: 'QMainWindow::QMainWindow' : cannot access private member declared in class 'QMainWindow'2> c:\qt\4.5.0\include\qtgui\../../src/gui/widgets/qmainwindow.h(206) : see declaration of 'QMainWindow::QMainWindow'2> c:\qt\4.5.0\include\qtgui\../../src/gui/widgets/qmainwindow.h(63) : see declaration of 'QMainWindow'2> This diagnostic occurred in the compiler generated function 'Calculator::Calculator(const Calculator &)'2>base_window.cpp2>.\base_window.cpp(18) : error C2511: 'void Base_window::openWidget(QListWidgetItem *)' : overloaded member function not found in 'Base_window'2> c:\base_window\base_window\base_window.h(11) : see declaration of 'Base_window'2>Generating Code...2>Build log was saved at "file://c:\Base_window\Base_window\Debug\BuildLog.htm"2>Base_window - 3 error(s), 0 warning(s)========== Build: 0 succeeded, 2 failed, 0 up-to-date, 0 skipped ==========
C++ (Qt)QHash<QString, QMetaObject*> class my_window : public QMainWindow{ Q_OBJECTpublic: Q_INVOKABLE my_window(QWidget* parent = 0); ...}; my_window::my_window(QWidget* parent) : QMainWindow(parent){ const QMetaObject* mo = metaObject(); prototypes[mo->className()] = mo;}...QObject* obj = prototypes["my_window"]->newInstance();QMainWindow* mw = qobject_cast<QMainWindow*>(obj);
C++ (Qt)#ifndef BASE_WINDOW_H#define BASE_WINDOW_H #include <QMainWindow>#include "ui_base_window.h"#include <QtGui>#include "../Calculator/calculator.h" using namespace Ui; class Base_window : public QMainWindow, public Base_windowClass{ Q_OBJECT public: Base_window(QWidget *parent = 0); ~Base_window();public slots: void Base_window::openWidget(QListWidgetItem * item) { QVariant w(QMetaType::type("Calculator")); //QMainWindow e=reinterpret_cast<QVariant> ( w);............... }}; #endif // BASE_WINDOW_H