bool cacheProperties;
};
-QQmlOpenMetaObject::QQmlOpenMetaObject(QObject *obj, bool automatic)
+QQmlOpenMetaObject::QQmlOpenMetaObject(QObject *obj, const QMetaObject *base, bool automatic)
: d(new QQmlOpenMetaObjectPrivate(this))
{
d->autoCreate = automatic;
d->object = obj;
- d->type = new QQmlOpenMetaObjectType(obj->metaObject(), 0);
+ d->type = new QQmlOpenMetaObjectType(base ? base : obj->metaObject(), 0);
d->type->d->referers.insert(this);
QObjectPrivate *op = QObjectPrivate::get(obj);
class Q_QML_PRIVATE_EXPORT QQmlOpenMetaObject : public QAbstractDynamicMetaObject
{
public:
- QQmlOpenMetaObject(QObject *, bool = true);
+ QQmlOpenMetaObject(QObject *, const QMetaObject * = 0, bool = true);
QQmlOpenMetaObject(QObject *, QQmlOpenMetaObjectType *, bool = true);
~QQmlOpenMetaObject();
class QQmlPropertyMapMetaObject : public QQmlOpenMetaObject
{
public:
- QQmlPropertyMapMetaObject(QQmlPropertyMap *obj, QQmlPropertyMapPrivate *objPriv);
+ QQmlPropertyMapMetaObject(QQmlPropertyMap *obj, QQmlPropertyMapPrivate *objPriv, const QMetaObject *staticMetaObject);
protected:
virtual QVariant propertyWriteValue(int, const QVariant &);
return keys[index];
}
-QQmlPropertyMapMetaObject::QQmlPropertyMapMetaObject(QQmlPropertyMap *obj, QQmlPropertyMapPrivate *objPriv) : QQmlOpenMetaObject(obj)
+QQmlPropertyMapMetaObject::QQmlPropertyMapMetaObject(QQmlPropertyMap *obj, QQmlPropertyMapPrivate *objPriv, const QMetaObject *staticMetaObject)
+ : QQmlOpenMetaObject(obj, staticMetaObject)
{
map = obj;
priv = objPriv;
\note It is not possible to remove keys from the map; once a key has been added, you can only
modify or clear its associated value.
+
+ \note When deriving a class from QQmlPropertyMap, use the
+ \l {QQmlPropertyMap::QQmlPropertyMap(DerivedType *derived, QObject *parent)}
+ {protected two-argument constructor}
+ which ensures that the class is correctly registered with the Qt \l {Meta-Object System}.
*/
/*!
Constructs a bindable map with parent object \a parent.
*/
QQmlPropertyMap::QQmlPropertyMap(QObject *parent)
-: QObject(*(new QQmlPropertyMapPrivate), parent)
+: QObject(*allocatePrivate(), parent)
{
- Q_D(QQmlPropertyMap);
- d->mo = new QQmlPropertyMapMetaObject(this, d);
+ init(metaObject());
}
/*!
*/
QVariant QQmlPropertyMap::updateValue(const QString &key, const QVariant &input)
{
+ Q_UNUSED(key)
return input;
}
+/*! \internal */
+void QQmlPropertyMap::init(const QMetaObject *staticMetaObject)
+{
+ Q_D(QQmlPropertyMap);
+ d->mo = new QQmlPropertyMapMetaObject(this, d, staticMetaObject);
+}
+
+/*! \internal */
+QObjectPrivate *QQmlPropertyMap::allocatePrivate()
+{
+ return new QQmlPropertyMapPrivate;
+}
+
/*!
\fn void QQmlPropertyMap::valueChanged(const QString &key, const QVariant &value)
This signal is emitted whenever one of the values in the map is changed. \a key
or clear() - it is only emitted when a value is updated from QML.
*/
+/*!
+ \fn QQmlPropertyMap::QQmlPropertyMap(DerivedType *derived, QObject *parent)
+
+ Constructs a bindable map with parent object \a parent. Use this constructor
+ in classes derived from QQmlPropertyMap.
+
+ The type of \a derived is used to register the property map with the \l {Meta-Object System},
+ which is necessary to ensure that properties of the derived class are accessible.
+ This type must be derived from QQmlPropertyMap.
+*/
+
QT_END_NAMESPACE
protected:
virtual QVariant updateValue(const QString &key, const QVariant &input);
+ template<class DerivedType>
+ QQmlPropertyMap(DerivedType *derived, QObject *parent = 0)
+ : QObject(*allocatePrivate(), parent)
+ {
+ Q_UNUSED(derived)
+ init(&DerivedType::staticMetaObject);
+ }
+
private:
+ void init(const QMetaObject *staticMetaObject);
+ static QObjectPrivate *allocatePrivate();
+
Q_DECLARE_PRIVATE(QQmlPropertyMap)
Q_DISABLE_COPY(QQmlPropertyMap)
};
void crashBug();
void QTBUG_17868();
+ void metaObjectAccessibility();
};
void tst_QQmlPropertyMap::insert()
}
+class MyEnhancedPropertyMap : public QQmlPropertyMap
+{
+ Q_OBJECT
+public:
+ MyEnhancedPropertyMap() : QQmlPropertyMap(this) {}
+
+signals:
+ void testSignal();
+
+public slots:
+ void testSlot() {}
+};
+
+namespace
+{
+ QStringList messages;
+ void msgHandler(QtMsgType, const char *msg)
+ {
+ messages << QLatin1String(msg);
+ }
+}
+
+void tst_QQmlPropertyMap::metaObjectAccessibility()
+{
+ messages.clear();
+ QtMsgHandler old = qInstallMsgHandler(msgHandler);
+
+ QQmlEngine engine;
+
+ MyEnhancedPropertyMap map;
+
+ // Verify that signals and slots of QQmlPropertyMap-derived classes are accessible
+ QObject::connect(&map, SIGNAL(testSignal()), &engine, SIGNAL(quit()));
+ QObject::connect(&engine, SIGNAL(quit()), &map, SLOT(testSlot()));
+
+ QCOMPARE(map.metaObject()->className(), "MyEnhancedPropertyMap");
+
+ qInstallMsgHandler(old);
+
+ QCOMPARE(messages.count(), 0);
+}
+
QTEST_MAIN(tst_QQmlPropertyMap)
#include "tst_qqmlpropertymap.moc"