Be able to read and write properties to Q_GADGET
Change-Id: Ic12f465d31459748ca08ac8c457fd61a5773e2e2 Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
This commit is contained in:
parent
ae981f224d
commit
d9e8571f4d
@ -2969,6 +2969,49 @@ bool QMetaProperty::reset(QObject *object) const
|
||||
QMetaObject::metacall(object, QMetaObject::ResetProperty, idx + mobj->propertyOffset(), argv);
|
||||
return true;
|
||||
}
|
||||
/*!
|
||||
\since 5.5
|
||||
|
||||
Reads the property's value from the given \a gadget. Returns the value
|
||||
if it was able to read it; otherwise returns an invalid variant.
|
||||
|
||||
This function should only be used if this is a property of a Q_GADGET
|
||||
*/
|
||||
QVariant QMetaProperty::readOnGadget(const void *gadget) const
|
||||
{
|
||||
Q_ASSERT(priv(mobj->d.data)->flags & PropertyAccessInStaticMetaCall && mobj->d.static_metacall);
|
||||
return read(reinterpret_cast<const QObject*>(gadget));
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.5
|
||||
|
||||
Writes \a value as the property's value to the given \a gadget. Returns
|
||||
true if the write succeeded; otherwise returns \c false.
|
||||
|
||||
This function should only be used if this is a property of a Q_GADGET
|
||||
*/
|
||||
bool QMetaProperty::writeOnGadget(void *gadget, const QVariant &value) const
|
||||
{
|
||||
Q_ASSERT(priv(mobj->d.data)->flags & PropertyAccessInStaticMetaCall && mobj->d.static_metacall);
|
||||
return write(reinterpret_cast<QObject*>(gadget), value);
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.5
|
||||
|
||||
Resets the property for the given \a gadget with a reset method.
|
||||
Returns \c true if the reset worked; otherwise returns \c false.
|
||||
|
||||
Reset methods are optional; only a few properties support them.
|
||||
|
||||
This function should only be used if this is a property of a Q_GADGET
|
||||
*/
|
||||
bool QMetaProperty::resetOnGadget(void *gadget) const
|
||||
{
|
||||
Q_ASSERT(priv(mobj->d.data)->flags & PropertyAccessInStaticMetaCall && mobj->d.static_metacall);
|
||||
return reset(reinterpret_cast<QObject*>(gadget));
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns \c true if this property can be reset to a default value; otherwise
|
||||
|
@ -1,6 +1,7 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
||||
** Copyright (C) 2014 Olivier Goffart <ogoffart@woboq.com>
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the QtCore module of the Qt Toolkit.
|
||||
@ -237,6 +238,10 @@ public:
|
||||
bool write(QObject *obj, const QVariant &value) const;
|
||||
bool reset(QObject *obj) const;
|
||||
|
||||
QVariant readOnGadget(const void *gadget) const;
|
||||
bool writeOnGadget(void *gadget, const QVariant &value) const;
|
||||
bool resetOnGadget(void *gadget) const;
|
||||
|
||||
bool hasStdCppSet() const;
|
||||
inline bool isValid() const { return isReadable(); }
|
||||
inline const QMetaObject *enclosingMetaObject() const { return mobj; }
|
||||
|
@ -1,6 +1,7 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
||||
** Copyright (C) 2014 Olivier Goffart <ogoffart@woboq.com>
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the test suite of the Qt Toolkit.
|
||||
@ -50,6 +51,7 @@ private slots:
|
||||
void hasStdCppSet();
|
||||
void isConstant();
|
||||
void isFinal();
|
||||
void gadget();
|
||||
|
||||
public:
|
||||
enum EnumType { EnumType1 };
|
||||
@ -103,5 +105,32 @@ void tst_QMetaProperty::isFinal()
|
||||
QVERIFY(!prop.isFinal());
|
||||
}
|
||||
|
||||
class MyGadget {
|
||||
Q_GADGET
|
||||
Q_PROPERTY(QString value READ getValue WRITE setValue RESET resetValue)
|
||||
public:
|
||||
QString m_value;
|
||||
void setValue(const QString &value) { m_value = value; }
|
||||
QString getValue() { return m_value; }
|
||||
void resetValue() { m_value = QLatin1Literal("reset"); }
|
||||
};
|
||||
|
||||
void tst_QMetaProperty::gadget()
|
||||
{
|
||||
const QMetaObject *mo = &MyGadget::staticMetaObject;
|
||||
QMetaProperty valueProp = mo->property(mo->indexOfProperty("value"));
|
||||
QVERIFY(valueProp.isValid());
|
||||
{
|
||||
MyGadget g;
|
||||
QString hello = QLatin1Literal("hello");
|
||||
QVERIFY(valueProp.writeOnGadget(&g, hello));
|
||||
QCOMPARE(g.m_value, QLatin1String("hello"));
|
||||
QCOMPARE(valueProp.readOnGadget(&g), QVariant(hello));
|
||||
QVERIFY(valueProp.resetOnGadget(&g));
|
||||
QCOMPARE(valueProp.readOnGadget(&g), QVariant(QLatin1String("reset")));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
QTEST_MAIN(tst_QMetaProperty)
|
||||
#include "tst_qmetaproperty.moc"
|
||||
|
Loading…
Reference in New Issue
Block a user