Qt 反射 newInstance

MarsCactus發表於2024-10-26

pragma once

include <qobject.h>

include

using namespace std;
class Person :
public QObject
{
Q_OBJECT
public:
Q_INVOKABLE Person(int age, string name)
{
this->_age = age;
this->_name = name;
}
Person(const Person& p) noexcept {
this->_age = p._age;
this->_name = p._name;
}
Person& operator =(const Person& p) noexcept {
this->_age = p._age;
this->_name = p._name;
return *this;
}
Person& operator =(Person&& p) noexcept {
this->_age = p._age;
this->_name = p._name;
return *this;
}
Person(Person&& p) noexcept {
this->_age = p._age;
this->_name = p._name;
}
Person() noexcept {}
~Person()
{}
friend ostream& operator<<(ostream& os, const Person& dt);

private:
int _age;
string _name;
};

//Q_DECLARE_METATYPE(Person)

include <QtCore/QCoreApplication>

include <Person.h>

include

include

using namespace std;
ostream& operator<<(ostream& os, const Person& dt)
{
os << "{ age: " << dt._age << " , name: " << dt._name << " }";
return os;
}
int main(int argc, char argv[])
{
QCoreApplication a(argc, argv);
const QMetaObject
p = &Person::staticMetaObject;
QVariant variant;

Person *person = (Person*)p->newInstance(12, std::string("lilei"));
variant.setValue<Person>(move(*person));
Person personobj = variant.value<Person>();
std::cout << personobj;
//std::cout << std::is_copy_constructible_v<Person>;
//std::cout << std::is_destructible_v<Person>;

return a.exec();

}

/****************************************************************************
** Meta object code from reading C++ file 'Person.h'
**
** Created by: The Qt Meta Object Compiler version 68 (Qt 6.7.2)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/

include "../../../../Person.h"

include <QtCore/qmetatype.h>

include <QtCore/qtmochelpers.h>

include

include <QtCore/qxptype_traits.h>

if !defined(Q_MOC_OUTPUT_REVISION)

error "The header file 'Person.h' doesn't include ."

elif Q_MOC_OUTPUT_REVISION != 68

error "This file was generated using the moc from 6.7.2. It"

error "cannot be used with the include files from this version of Qt."

error "(The moc has changed too much.)"

endif

ifndef Q_CONSTINIT

define Q_CONSTINIT

endif

QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
QT_WARNING_DISABLE_GCC("-Wuseless-cast")
namespace {

ifdef QT_MOC_HAS_STRINGDATA

struct qt_meta_stringdata_CLASSPersonENDCLASS_t {};
constexpr auto qt_meta_stringdata_CLASSPersonENDCLASS = QtMocHelpers::stringData(
"Person",
"",
"age",
"string",
"name"
);

else // !QT_MOC_HAS_STRINGDATA

error "qtmochelpers.h not found or too old."

endif // !QT_MOC_HAS_STRINGDATA

} // unnamed namespace

Q_CONSTINIT static const uint qt_meta_data_CLASSPersonENDCLASS[] = {

// content:
12, // revision
0, // classname
0, 0, // classinfo
0, 0, // methods
0, 0, // properties
0, 0, // enums/sets
1, 19, // constructors
0, // flags
0, // signalCount

// constructors: parameters
0x80000000 | 1, QMetaType::Int, 0x80000000 | 3, 2, 4,

// constructors: name, argc, parameters, tag, flags, initial metatype offsets
0, 2, 14, 1, 0x0e, 1 /* Public */,

   0        // eod

};

Q_CONSTINIT const QMetaObject Person::staticMetaObject = { {
QMetaObject::SuperData::linkQObject::staticMetaObject(),
qt_meta_stringdata_CLASSPersonENDCLASS.offsetsAndSizes,
qt_meta_data_CLASSPersonENDCLASS,
qt_static_metacall,
nullptr,
qt_incomplete_metaTypeArray<qt_meta_stringdata_CLASSPersonENDCLASS_t,
// Q_OBJECT / Q_GADGET
QtPrivate::TypeAndForceComplete<Person, std::true_type>,
// constructor 'Person'
QtPrivate::TypeAndForceComplete<int, std::false_type>,
QtPrivate::TypeAndForceComplete<string, std::false_type>
>,
nullptr
} };

void Person::qt_static_metacall(QObject _o, QMetaObject::Call _c, int _id, void **_a)
{
if (_c == QMetaObject::CreateInstance) {
switch (_id) {
case 0: { Person _r = new Person((reinterpret_cast<std::add_pointer_t>(_a[1])),(
reinterpret_cast<std::add_pointer_t>(_a[2])));
if (_a[0]) reinterpret_cast<QObject**>(_a[0]) = _r; } break;
default: break;
}
} else if (_c == QMetaObject::ConstructInPlace) {
switch (_id) {
case 0: { new (_a[0]) Person((
reinterpret_cast<std::add_pointer_t>(_a[1])),(*reinterpret_cast<std::add_pointer_t>(_a[2]))); } break;
default: break;
}
}
(void)_o;
}

const QMetaObject *Person::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
}

void *Person::qt_metacast(const char _clname)
{
if (!_clname) return nullptr;
if (!strcmp(_clname, qt_meta_stringdata_CLASSPersonENDCLASS.stringdata0))
return static_cast<void
>(this);
return QObject::qt_metacast(_clname);
}

int Person::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QObject::qt_metacall(_c, _id, _a);
return _id;
}
QT_WARNING_POP

相關文章