bool UiExtractor::checkProperty(QObject *obj, const QString &prop) const { const QMetaObject *mo = obj->metaObject(); const QMetaProperty mp = mo->property(mo->indexOfProperty(prop.toLatin1())); // TODO come up with some more aggressive filtering if (mp.isValid() && mp.isDesignable(obj) && mp.isStored(obj) && mp.isWritable()) { const QVariant value = mp.read(obj); // try to figure out the default by resetting to it if (mp.isResettable()) { mp.reset(obj); if (mp.read(obj) == value) { return false; } mp.write(obj, value); return true; } // some guessing for non-resettable properties if (value.isNull() || !value.isValid()) { return false; } if (value.type() == QVariant::String) { return !value.toString().isEmpty(); } else if (value.type() == QVariant::Locale) { return value.value<QLocale>() != QLocale::system(); } return true; } return false; }
void ObjectControllerPrivate::updateClassProperties(const QMetaObject *metaObject, bool recursive) { if (!metaObject) return; if (recursive) updateClassProperties(metaObject->superClass(), recursive); QtProperty *classProperty = m_classToProperty.value(metaObject); if (!classProperty) return; for (int idx = metaObject->propertyOffset(); idx < metaObject->propertyCount(); idx++) { QMetaProperty metaProperty = metaObject->property(idx); if (metaProperty.isReadable()) { if (m_classToIndexToProperty.contains(metaObject) && m_classToIndexToProperty[metaObject].contains(idx)) { QtVariantProperty *subProperty = m_classToIndexToProperty[metaObject][idx]; if (metaProperty.isEnumType()) { if (metaProperty.isFlagType()) subProperty->setValue(flagToInt(metaProperty.enumerator(), metaProperty.read(m_object).toInt())); else subProperty->setValue(enumToInt(metaProperty.enumerator(), metaProperty.read(m_object).toInt())); } else { subProperty->setValue(metaProperty.read(m_object)); } } } } }
QVariant ObjectStaticPropertyModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || !m_obj || index.row() < 0 || index.row() >= m_obj.data()->metaObject()->propertyCount()) { return QVariant(); } const QMetaProperty prop = m_obj.data()->metaObject()->property(index.row()); if (role == Qt::DisplayRole) { if (index.column() == 0) { return prop.name(); } else if (index.column() == 1) { // QMetaProperty::read sets QVariant::typeName to int for enums, // so we need to handle that separately here const QVariant value = prop.read(m_obj.data()); const QString enumStr = Util::enumToString(value, prop.typeName(), m_obj.data()); if (!enumStr.isEmpty()) { return enumStr; } return VariantHandler::displayString(value); } else if (index.column() == 2) { return prop.typeName(); } else if (index.column() == 3) { const QMetaObject *mo = m_obj.data()->metaObject(); while (mo->propertyOffset() > index.row()) { mo = mo->superClass(); } return mo->className(); } } else if (role == Qt::DecorationRole) { if (index.column() == 1) { return VariantHandler::decoration(prop.read(m_obj.data())); } } else if (role == Qt::EditRole) { if (index.column() == 1) { return prop.read(m_obj.data()); } } else if (role == Qt::ToolTipRole) { const QString toolTip = tr("Constant: %1\nDesignable: %2\nFinal: %3\nResetable: %4\n" "Has notification: %5\nScriptable: %6\nStored: %7\nUser: %8\nWritable: %9"). arg(translateBool(prop.isConstant())). arg(translateBool(prop.isDesignable(m_obj.data()))). arg(translateBool(prop.isFinal())). arg(translateBool(prop.isResettable())). arg(translateBool(prop.hasNotifySignal())). arg(translateBool(prop.isScriptable(m_obj.data()))). arg(translateBool(prop.isStored(m_obj.data()))). arg(translateBool(prop.isUser(m_obj.data()))). arg(translateBool(prop.isWritable())); return toolTip; } return QVariant(); }
void NodeInstanceSignalSpy::registerObject(QObject *spiedObject, const PropertyName &prefix) { if (m_registeredObjectList.contains(spiedObject)) // prevent cycles return; m_registeredObjectList.append(spiedObject); for (int index = QObject::staticMetaObject.propertyOffset(); index < spiedObject->metaObject()->propertyCount(); index++) { QMetaProperty metaProperty = spiedObject->metaObject()->property(index); // handle dot properties and connect the signals to the object if (metaProperty.isReadable() && !metaProperty.isWritable() && QDeclarativeMetaType::isQObject(metaProperty.userType())) { QObject *propertyObject = QDeclarativeMetaType::toQObject(metaProperty.read(spiedObject)); if (propertyObject) registerObject(propertyObject, prefix + metaProperty.name() + '.'); } else if (metaProperty.hasNotifySignal()) { QMetaMethod metaMethod = metaProperty.notifySignal(); bool isConnecting = QMetaObject::connect(spiedObject, metaMethod.methodIndex(), this, methodeOffset, Qt::DirectConnection); Q_ASSERT(isConnecting); Q_UNUSED(isConnecting); m_indexPropertyHash.insert(methodeOffset, prefix + metaProperty.name()); methodeOffset++; } // search recursive in objects if (metaProperty.isReadable() && metaProperty.isWritable() && QDeclarativeMetaType::isQObject(metaProperty.userType())) { QObject *propertyObject = QDeclarativeMetaType::toQObject(metaProperty.read(spiedObject)); if (propertyObject) registerObject(propertyObject, prefix + metaProperty.name() + '/'); } // search recursive in objects list if (metaProperty.isReadable() && QDeclarativeMetaType::isList(metaProperty.userType())) { QDeclarativeListReference list(spiedObject, metaProperty.name()); if (list.canCount() && list.canAt()) { for (int i = 0; i < list.count(); i++) { QObject *propertyObject = list.at(i); if (propertyObject) registerObject(propertyObject, prefix + metaProperty.name() + '/'); } } } } }
bool WriteMetaProperty(QObject *pObject, QString pName, const QVariant& pValue) { if (!pObject) return false; const QMetaObject *metaObject = pObject->metaObject(); int index = metaObject->indexOfProperty(pName.toUtf8().constData()); if (index >= 0) { QMetaProperty metaProperty = metaObject->property(index); if (metaProperty.isWritable() && //Value cant Writed metaProperty.read(pObject) != pValue) //Value changed { metaProperty.write(pObject, pValue); return true; } else { return false; } } else { return false; } }
QVariant QObject::property(const char *name) const { const QMetaObject* metaObj = metaObject(); if (! name || ! metaObj) { return QVariant(); } int index = metaObj->indexOfProperty(name); if (index < 0) { const int k = m_extra_propertyNames.indexOf(name); if (k == -1) { return QVariant(); } return m_extra_propertyValues.value(k); } QMetaProperty p = metaObj->property(index); if (! p.isReadable()) { qWarning("%s::property() Property \"%s\" is invalid or does not exist", metaObj->className(), name); } return p.read(this); }
void QuickConfig::syncProperties(QObject *object) { const QMetaObject * const base = object == this ? &staticMetaObject : &QObject::staticMetaObject; const QMetaObject * const actual = object->metaObject(); int propertiesCount = actual->propertyCount(); for (int index = base->propertyCount(); index < propertiesCount; ++index) { QMetaProperty property = actual->property(index); QVariant defaultValue = property.read(object); QMetaType type(property.userType()); if (type.flags() & QMetaType::PointerToQObject) { QObject *subObject = defaultValue.value<QObject *>(); if (!subObject) { qWarning() << "QuickConfig: null value at" << this << ", property:" << property.name(); continue; } m_config.beginGroup(QLatin1String(property.name())); syncProperties(subObject); m_config.endGroup(); continue; } ConfigValue<QVariant> actualValue = m_config.value(QLatin1String(property.name()), defaultValue); property.write(object, actualValue.value()); // Store actualValue as it's destruction will drop onChange listener new QuickConfigListener(m_path, m_group, property, object, actualValue, this); actualValue.onChange(object, [object, property, defaultValue] (const QVariant &value) { property.write(object, value.isValid() ? value : defaultValue); }); } }
void CopyMetaProperties(QObject *pObject, QObject *pTarget) { //Temp const QMetaObject *metaOrigin = pObject->metaObject(); const QMetaObject *metaTarget = pTarget->metaObject(); QMetaProperty metaPropertyOrigin; QMetaProperty metaPropertyTarget; QVariant value; int index; int max = metaOrigin->propertyCount(); bool denied; //Read pObject property and Write pTarget property if is writetable for (int i = 0; i < max; i++) { metaPropertyOrigin = metaOrigin->property(i); const char *name = metaPropertyOrigin.name(); index = metaTarget->indexOfProperty(name); denied = QString("selected").split(",").contains(name); if (index >= 0 && !denied) { metaPropertyTarget = metaTarget->property(index); if (metaPropertyTarget.isWritable()) { value = metaPropertyOrigin.read(pObject);//Read metaPropertyTarget.write(pTarget, value);//Write } } } }
void AppSettings::objectChanged(QObject *object, int index) { QMultiHash<QObject*,QPair<QString,int> >::Iterator it = trackingObjectMap.find(object), itstart = it; if (it == trackingObjectMap.end()) return; /* Index is the index of this property among the properties connected from this object. * In the QMultiHash, these are stored from the most-recently inserted to the least recently * inserted, so we need to invert the number and get the entry at that offset. */ int propCount = 0; for (; it != trackingObjectMap.end() && it.key() == object; ++it, ++propCount) ; /* First connection (index=0) is the last in the map (propCount-1) */ index = propCount - 1 - index; it = itstart + index; if (it->second < 0) return; QMetaProperty property = object->metaObject()->property(it->second); if (!property.isValid()) return; QObject *old = keyChangeSource; keyChangeSource = object; setValue(it->first, property.read(object)); keyChangeSource = old; }
void QNodePrivate::propertyChanged(int propertyIndex) { // Bail out early if we can to avoid the cost below if (m_blockNotifications) return; Q_Q(QNode); const QMetaProperty property = q->metaObject()->property(propertyIndex); const QVariant data = property.read(q); if (data.canConvert<QNode*>()) { QNode *node = data.value<QNode*>(); // Ensure the node has issued a node creation change. We can end // up here if a newly created node with a parent is immediately set // as a property on another node. In this case the deferred call to // _q_postConstructorInit() will not have happened yet as the event // loop will still be blocked. So force it here and we catch this // eventuality in the _q_postConstructorInit() function so that we // do not repeat the creation and new child scene change events. if (node) QNodePrivate::get(node)->_q_postConstructorInit(); const QNodeId id = node ? node->id() : QNodeId(); notifyPropertyChange(property.name(), QVariant::fromValue(id)); } else { notifyPropertyChange(property.name(), data); } }
static QVariantMap readAllProperties(QObject *object, int flags) { QVariantMap result; const QMetaObject *mo = object->metaObject(); // QObject has properties, so don't start from 0 for (int i = QObject::staticMetaObject.propertyCount(); i < mo->propertyCount(); ++i) { QMetaProperty mp = mo->property(i); // is it readable? if (!mp.isReadable()) continue; // is it a registered property? int typeId = mp.userType(); if (!typeId) continue; const char *signature = QDBusMetaType::typeToSignature(typeId); if (!signature) continue; // is this property visible from the outside? if ((mp.isScriptable() && flags & QDBusConnection::ExportScriptableProperties) || (!mp.isScriptable() && flags & QDBusConnection::ExportNonScriptableProperties)) { // yes, it's visible QVariant value = mp.read(object); if (value.isValid()) result.insert(QString::fromLatin1(mp.name()), value); } } return result; }
void Message::serialize(QTextStream& stream) const { const QMetaObject* object = metaObject(); stream << object->className() << "("; bool firstProperty = true; while (object && QLatin1String(object->className()) != QLatin1String("QObject")) { for(int i = object->propertyOffset(); i < object->propertyCount(); ++i) { if (!firstProperty) stream << ", "; firstProperty = false; QMetaProperty property = object->property(i); QString name = property.name(); name.replace(0, 1, name.at(0).toUpper()); stream << name; stream << ":"; QString value = property.read(this).toString(); if (value.length() > 10) { value.truncate(10); value.append("..."); } if (value.isEmpty()) value = "0"; stream << value; } object = object->superClass(); } stream << ")"; }
void collectReachableMetaObjects(QObject *object, QSet<const QMetaObject *> *metas) { if (! object) return; const QMetaObject *meta = object->metaObject(); if (verbose) qDebug() << "Processing object" << meta->className(); collectReachableMetaObjects(meta, metas); for (int index = 0; index < meta->propertyCount(); ++index) { QMetaProperty prop = meta->property(index); if (QDeclarativeMetaType::isQObject(prop.userType())) { if (verbose) qDebug() << " Processing property" << prop.name(); currentProperty = QString("%1::%2").arg(meta->className(), prop.name()); // if the property was not initialized during construction, // accessing a member of oo is going to cause a segmentation fault QObject *oo = QDeclarativeMetaType::toQObject(prop.read(object)); if (oo && !metas->contains(oo->metaObject())) collectReachableMetaObjects(oo, metas); currentProperty.clear(); } } }
void print_object_props(QObject * object, QTextStream & stream) { for (int i = 0; i < object->metaObject()->propertyCount(); ++i) { QMetaProperty property = object->metaObject()->property(i); QString strValue = property.read(object).toString(); if (!strValue.isEmpty()) { stream << "\t" << property.name() << ": " << strValue << endl; } } }
QVariant ObjectStaticPropertyModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || !m_obj || index.row() < 0 || index.row() >= m_obj.data()->metaObject()->propertyCount()) { return QVariant(); } const QMetaProperty prop = m_obj.data()->metaObject()->property(index.row()); if (role == Qt::DisplayRole) { if (index.column() == 0) { return prop.name(); } else if (index.column() == 1) { return Util::variantToString(prop.read(m_obj.data())); } else if (index.column() == 2) { return prop.typeName(); } else if (index.column() == 3) { const QMetaObject *mo = m_obj.data()->metaObject(); while (mo->propertyOffset() > index.row()) { mo = mo->superClass(); } return mo->className(); } } else if (role == Qt::EditRole) { if (index.column() == 1) { return prop.read(m_obj.data()); } } else if (role == Qt::ToolTipRole) { const QString toolTip = tr("Constant: %1\nDesignable: %2\nFinal: %3\nResetable: %4\n" "Has notification: %5\nScriptable: %6\nStored: %7\nUser: %8\nWritable: %9"). arg(translateBool(prop.isConstant())). arg(translateBool(prop.isDesignable(m_obj.data()))). arg(translateBool(prop.isFinal())). arg(translateBool(prop.isResettable())). arg(translateBool(prop.hasNotifySignal())). arg(translateBool(prop.isScriptable(m_obj.data()))). arg(translateBool(prop.isStored(m_obj.data()))). arg(translateBool(prop.isUser(m_obj.data()))). arg(translateBool(prop.isWritable())); return toolTip; } return QVariant(); }
void Preferences::save() const { QSettings settings( VIEWER_APPLICATION_VENDOR, VIEWER_APPLICATION_NAME ); const QMetaObject * mo = metaObject(); for( int i = 0; i < mo->propertyCount(); i ++ ) { QMetaProperty mp = mo->property( i ); settings.setValue( mp.name(), mp.read(this) ); } }
bool DeclarativeDBusAdaptor::handleMessage(const QDBusMessage &message, const QDBusConnection &connection) { QVariant variants[10]; QGenericArgument arguments[10]; const QMetaObject * const meta = metaObject(); const QVariantList dbusArguments = message.arguments(); QString member = message.member(); QString interface = message.interface(); // Don't try to handle introspect call. It will be handled // internally for QDBusVirtualObject derived classes. if (interface == QLatin1String("org.freedesktop.DBus.Introspectable")) { return false; } else if (interface == QLatin1String("org.freedesktop.DBus.Properties")) { if (member == QLatin1String("Get")) { interface = dbusArguments.value(0).toString(); member = dbusArguments.value(1).toString(); const QMetaObject * const meta = metaObject(); if (!member.isEmpty() && member.at(0).isUpper()) member = "rc" + member; for (int propertyIndex = meta->propertyOffset(); propertyIndex < meta->propertyCount(); ++propertyIndex) { QMetaProperty property = meta->property(propertyIndex); if (QLatin1String(property.name()) != member) continue; QVariant value = property.read(this); if (value.userType() == qMetaTypeId<QJSValue>()) value = value.value<QJSValue>().toVariant(); if (value.userType() == QVariant::List) { QVariantList variantList = value.toList(); if (variantList.count() > 0) { QDBusArgument list; list.beginArray(variantList.first().userType()); foreach (const QVariant &listValue, variantList) { list << listValue; } list.endArray(); value = QVariant::fromValue(list); } } QDBusMessage reply = message.createReply(QVariantList() << value); connection.call(reply, QDBus::NoBlock); return true; }
void QJnextMainLoop::get(QObject *object, QVariantList& args, QByteArray* retval) { QByteArray property = args.takeFirst().toByteArray(); const QMetaObject * meta = object->metaObject(); int propertyIndex = meta->indexOfProperty(property); if (propertyIndex < 0) { retval->append("No such property " + property); return; } QMetaProperty metaprop = meta->property(propertyIndex); if (!metaprop.isReadable()) { retval->append("Property " + property + " is not readable"); return; } QVariant value = metaprop.read(object); if (value.isNull()) { qWarning() << "[QJnextMainLoop]\tNULL value ignored" << object->property(property); } #ifdef DEBUG_QJnextMainLoop qDebug() << "[QJnextMainLoop]\tVALUE" << value << object->property(property) << metaprop.isEnumType(); #endif if (metaprop.isEnumType()) { bool ok; int enumValue = value.toInt(&ok); if (!ok) { int status = -1; void *argv[] = { 0, &value, &status }; QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::ReadProperty, propertyIndex + meta->propertyOffset(), argv); const int * enumRaw = static_cast<const int *>(argv[0]); if (status == -1 && enumRaw != 0) { const QMetaEnum & iEnum = metaprop.enumerator(); if (iEnum.isFlag()) value = iEnum.valueToKeys(*enumRaw); else value = iEnum.valueToKey(*enumRaw); } else { // someone is evil and didn't register enumerations properly qDebug() << "[QJnextMainLoop]\t" << "!!!!!!!" << argv[0] << value; } } else if (metaprop.enumerator().isFlag()) { *retval = metaprop.enumerator().valueToKeys(enumValue); return; } else { *retval = metaprop.enumerator().valueToKey(enumValue); return; } } *retval = value.toByteArray(); }
QtFlickProvider::QtFlickProvider(QQuickWebView* viewItem, QQuickWebPage* pageItem) : QObject(viewItem) { ASSERT(viewItem); ASSERT(pageItem); QDeclarativeEngine* engine = qmlEngine(viewItem); QDeclarativeContext* context = qmlContext(viewItem); ASSERT(engine); ASSERT(context); QDeclarativeComponent component(engine, viewItem); // Create the internal Flickable instance dynamically. // We only use public QML API so that we do not depend // on private API of the QtDeclarative module. component.setData(QByteArrayLiteral("import QtQuick 2.0\nFlickable {}"), QUrl()); m_flickable = qobject_cast<QQuickItem*>(component.create(context)); QMetaProperty content = resolveMetaProperty(m_flickable, "contentItem"); m_contentItem = content.read(m_flickable).value<QQuickItem*>(); ASSERT(m_contentItem); // Resolve meta methods and properties of the Flickable instance. m_returnToBoundsMethod = resolveMetaMethod(m_flickable, "returnToBounds()"); m_cancelFlickMethod = resolveMetaMethod(m_flickable, "cancelFlick()"); m_contentWidth = resolveMetaProperty(m_flickable, "contentWidth"); m_contentHeight = resolveMetaProperty(m_flickable, "contentHeight"); m_contentX = resolveMetaProperty(m_flickable, "contentX"); m_contentY = resolveMetaProperty(m_flickable, "contentY"); m_moving = resolveMetaProperty(m_flickable, "moving"); m_dragging = resolveMetaProperty(m_flickable, "dragging"); m_flicking = resolveMetaProperty(m_flickable, "flicking"); m_flickableData = resolveMetaProperty(m_flickable, "flickableData"); // Set the viewItem as the parent of the flickable instance // and reparent the page so it is placed on the flickable contentItem. m_flickable->setParentItem(viewItem); pageItem->setParentItem(m_contentItem); // Propagate flickable signals. connect(m_flickable, SIGNAL(movementStarted()), SIGNAL(movementStarted()), Qt::DirectConnection); connect(m_flickable, SIGNAL(movementEnded()), SIGNAL(movementEnded()), Qt::DirectConnection); connect(m_flickable, SIGNAL(flickingChanged()), SIGNAL(flickingChanged()), Qt::DirectConnection); connect(m_flickable, SIGNAL(draggingChanged()), SIGNAL(draggingChanged()), Qt::DirectConnection); connect(m_flickable, SIGNAL(contentWidthChanged()), SIGNAL(contentWidthChanged()), Qt::DirectConnection); connect(m_flickable, SIGNAL(contentHeightChanged()), SIGNAL(contentHeightChanged()), Qt::DirectConnection); connect(m_flickable, SIGNAL(contentXChanged()), SIGNAL(contentXChanged()), Qt::DirectConnection); connect(m_flickable, SIGNAL(contentYChanged()), SIGNAL(contentYChanged()), Qt::DirectConnection); }
QVariantMap NoronJsonBinarySerializer::serializeQObject(QObject *obj) { QVariantMap map; for(int i = 0; i < obj->metaObject()->propertyCount(); i++){ QMetaProperty property = obj->metaObject()->property(i); if(property.isReadable() && property.isWritable()) map.insert(property.name(), property.read(obj).toString()); } return map; }
void Message::serialize(QDataStream& stream) const { const QMetaObject* object = metaObject(); while (object && QLatin1String(object->className()) != QLatin1String("QObject")) { for(int i = object->propertyOffset(); i < object->propertyCount(); ++i) { QMetaProperty property = object->property(i); if (property.isStored(this)) stream << property.read(this); } object = object->superClass(); } }
QDeclarativeValueTypeScriptClass::Value QDeclarativeValueTypeScriptClass::property(Object *obj, const Identifier &) { QDeclarativeValueTypeObject *o = static_cast<QDeclarativeValueTypeObject *>(obj); QVariant rv; if (o->objectType == QDeclarativeValueTypeObject::Reference) { QDeclarativeValueTypeReference *ref = static_cast<QDeclarativeValueTypeReference *>(obj); QMetaProperty p = ref->type->metaObject()->property(m_lastIndex); ref->type->read(ref->object, ref->property); rv = p.read(ref->type); } else { QDeclarativeValueTypeCopy *copy = static_cast<QDeclarativeValueTypeCopy *>(obj); QMetaProperty p = copy->type->metaObject()->property(m_lastIndex); copy->type->setValue(copy->value); rv = p.read(copy->type); } QScriptEngine *scriptEngine = QDeclarativeEnginePrivate::getScriptEngine(engine); return Value(scriptEngine, static_cast<QDeclarativeEnginePrivate *>(QObjectPrivate::get(engine))->scriptValueFromVariant(rv)); }
void updateVisible() { bool newVisible = false; if (visibleProperty.isValid()) { Q_ASSERT(dialog.data()); newVisible = visibleProperty.read(dialog.data()).toBool(); } if (newVisible != visible) { visible = newVisible; emit q_ptr->visibleChanged(visible); } }
QStringList propertyNameForWritableProperties(QObject *object, const QString &baseName = QString()) { QStringList propertyNameList; const QMetaObject *metaObject = object->metaObject(); for (int index = 0; index < metaObject->propertyCount(); ++index) { QMetaProperty metaProperty = metaObject->property(index); if (metaProperty.isReadable() && !metaProperty.isWritable()) { QObject *childObject = QDeclarativeMetaType::toQObject(metaProperty.read(object)); if (childObject) propertyNameList.append(propertyNameForWritableProperties(childObject, baseName + QString::fromUtf8(metaProperty.name()) + '.')); } else if (QDeclarativeValueTypeFactory::valueType(metaProperty.userType())) { QDeclarativeValueType *valueType = QDeclarativeValueTypeFactory::valueType(metaProperty.userType()); valueType->setValue(metaProperty.read(object)); propertyNameList.append(propertyNameForWritableProperties(valueType, baseName + QString::fromUtf8(metaProperty.name()) + '.')); } else if (metaProperty.isReadable() && metaProperty.isWritable()) { propertyNameList.append(baseName + QString::fromUtf8(metaProperty.name())); } } return propertyNameList; }
/*! Returns the value of the property at \a index. Note that this function is slower than calling the data function directly. Here is an example of getting a property via the different mechanisms available. Accessing directly provides the best performance but requires compile-time knowledge of the data you are accessing. \code QAccelerometerReading *reading = ...; qreal x = reading->x(); \endcode You can also access a property by name. To do this you must call QObject::property(). \code qreal x = reading->property("x").value<qreal>(); \endcode Finally, you can access values via numeric index. \code qreal x = reading->value(0).value<qreal>(); \endcode Note that value() can only access properties declared with Q_PROPERTY() in sub-classes of QSensorReading. \sa valueCount(), QObject::property() */ QVariant QSensorReading::value(int index) const { // get them meta-object const QMetaObject *mo = metaObject(); // determine the index of the property we want index += mo->propertyOffset(); // get the meta-property QMetaProperty property = mo->property(index); // read the property return property.read(this); }
HDF *ClearSilverPrivate::hdfForStash(Context *c, const QVariantHash &stash) const { HDF *hdf = 0; hdf_init(&hdf); serializeHash(hdf, stash); const QMetaObject *meta = c->metaObject(); for (int i = 0; i < meta->propertyCount(); ++i) { QMetaProperty prop = meta->property(i); QString name = QLatin1String("c.") + QString::fromLatin1(prop.name()); QVariant value = prop.read(c); serializeVariant(hdf, value, name); } return hdf; }
void ObjectEndPoint::propertyCall(const QServicePackage& p) { if(p.d->responseType == QServicePackage::NotAResponse) { //service side Q_ASSERT(d->endPointType == ObjectEndPoint::Service); QByteArray data = p.d->payload.toByteArray(); QDataStream stream(&data, QIODevice::ReadOnly); int metaIndex = -1; QVariant arg; int callType; stream >> metaIndex; stream >> arg; stream >> callType; const QMetaObject::Call c = (QMetaObject::Call) callType; QVariant result; QMetaProperty property = service->metaObject()->property(metaIndex); if (property.isValid()) { switch(c) { case QMetaObject::ReadProperty: result = property.read(service); break; case QMetaObject::WriteProperty: property.write(service, arg); break; case QMetaObject::ResetProperty: property.reset(service); break; default: break; } } if (c == QMetaObject::ReadProperty) { QServicePackage response = p.createResponse(); if (property.isValid()) { response.d->responseType = QServicePackage::Success; response.d->payload = result; } else { response.d->responseType = QServicePackage::Failed; } dispatch->writePackage(response); } } else {
QVariant ReadMetaProperty(QObject *pObject, QString pName) { if (pObject) { QMetaProperty metaProperty; const QMetaObject *metaObject = pObject->metaObject(); int max = metaObject->propertyCount(); for (int i = 0; i < max; i++) { metaProperty = metaObject->property(i); if (pName == metaProperty.name()) return metaProperty.read(pObject); } } else { return QVariant(); } }
void PropertyEditor::getProperties(QObject *obj) { mMetaProperties.clear(); mProperties.clear(); if (!obj) return; const QMetaObject *mo = obj->metaObject(); for (int i = 0; i < mo->propertyCount(); ++i) { QMetaProperty mp = mo->property(i); mMetaProperties.append(mp); QVariant v(mp.read(obj)); if (mp.isEnumType()) { mProperties.insert(mp.name(), v.toInt());//mp.enumerator().valueToKey(v.toInt())); //always use string } else { mProperties.insert(mp.name(), v); } } mProperties.remove("objectName"); }
QDomElement BorderDrawersLoader::drawerToSvg(BorderDrawerInterface * drawer, QDomDocument & document) { if (!drawer) return QDomElement(); QDomElement result = document.createElement("g"); result.setAttribute("name", drawer->factory()->drawerName()); result.appendChild( drawer->toSvg(document) ); const QMetaObject * meta = drawer->metaObject(); int count = meta->propertyCount(); for (int i = 0; i < count; ++i) { QMetaProperty p = meta->property(i); result.setAttribute( p.name(), QString(p.read(drawer).toByteArray().toBase64()) ); } return result; }