Meta-Object System
How Nt describes classes at run time: introspection, properties and invoking by name.
A class that contains Q_OBJECT (or Q_GADGET) and goes through the ntmoc tool has a QMetaObject. ntmoc reads the class declaration and records:
the class name and the base class;
signals, slots and functions marked
Q_INVOKABLE, with their parameter types and names, and their default arguments;constructors marked
Q_INVOKABLE;properties declared with
Q_PROPERTY();enumerations declared with
Q_ENUM()andQ_FLAG();class information declared with
Q_CLASSINFO().
Using it
class Counter : public QObject {
Q_OBJECT
Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
Q_CLASSINFO("Version", "1.0")
public:
enum Mode { Up, Down };
Q_ENUM(Mode)
int value() const { return m_value; }
Q_INVOKABLE void reset() { setValue(0); }
public slots:
void setValue(int v);
signals:
void valueChanged(int value);
private:
int m_value = 0;
};
Counter c;
c.setProperty("value", 5); // via the property system
QMetaObject::invokeMethod(&c, "reset"); // by name
c.metaObject()->className(); // "Counter"
QObject::connect(&c, SIGNAL(valueChanged(int)), &other, SLOT(setValue(int)));
Properties
QObject::property() and QObject::setProperty() read and write properties by name, converting values through QVariant. A name that is not a declared property creates a dynamic property on the object; its changes are reported with a QDynamicPropertyChangeEvent.
Value types
QMetaType describes the types that properties, method arguments and QVariant hold. Types that Nt does not know need Q_DECLARE_METATYPE() or qRegisterMetaType() before they can be queued, stored in a QVariant or looked up by name.
Limits
The meta-object of a class exists only if ntmoc processed its header. Code that does not use ntmoc, such as a class that only declares signals with emitSignal() bodies, keeps working but has no QMetaObject of its own: QObject::metaObject() returns that of its nearest base class that has one. ntmoc is a text scanner and does not evaluate the preprocessor, so declarations produced by macros are not seen.
See also QMetaObject, QMetaType, QVariant and Signals and Slots.