void QpMetaObject::initProperties() const { QMutexLocker m(data->mutex); Q_UNUSED(m); int count = data->metaObject.propertyCount(); for (int i = 1; i < count; ++i) { QMetaProperty p = data->metaObject.property(i); QpMetaProperty mp = QpMetaProperty(p, *this); if (mp.isCalculated()) { data->calculatedProperties.append(mp); } if (!p.isStored()) continue; data->metaPropertiesByName.insert(p.name(), mp); data->metaProperties.append(mp); if (mp.isRelationProperty()) { data->relationProperties.append(mp); } else { data->simpleProperties.append(mp); } } }
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; }
bool mafObjectBase::isEqual(const mafObjectBase *obj) const { if (obj == NULL) { return false; } const QMetaObject* my_meta = metaObject(); const QMetaObject* obj_meta = obj->metaObject(); int i = 0; int num = obj_meta->propertyCount(); int checkNum = my_meta->propertyCount(); if(num != checkNum) { return false; } for ( ; i < num; ++i) { const QMetaProperty obj_qmp = obj_meta->property(i); const QMetaProperty my_qmp = my_meta->property(i); QString obj_name = obj_qmp.name(); if(obj_name == "objectHash" || !my_qmp.isStored()) { continue; } QVariant obj_value = obj->property(obj_name.toAscii()); QString my_name = my_qmp.name(); QVariant my_value = property(my_name.toAscii()); QByteArray ba = my_name.toAscii(); char *n = ba.data(); if((my_name != obj_name) || (my_value != obj_value)) { qDebug() << my_name; qDebug() << obj_name; qDebug() << my_value; qDebug() << obj_value; return false; } } return true; }
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 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(); } }
mafMementoObject::mafMementoObject(const mafObject *obj, const QString code_location) : mafMemento(obj, code_location) { mafMementoPropertyList *list = mementoPropertyList(); int i = 0; const QMetaObject *meta = obj->metaObject(); int num = meta->propertyCount(); for ( ; i < num; ++i) { mafMementoPropertyItem item; const QMetaProperty qmp = meta->property(i); if(!qmp.isStored()) { continue; } QString propName = qmp.name(); QVariant value = obj->property(propName.toAscii()); if (value.canConvert(QVariant::List)) { item.m_Multiplicity = value.toList().count(); } else if(value.canConvert(QVariant::Map)){ item.m_Multiplicity = value.toMap().count(); } else if(value.canConvert(QVariant::Hash)){ item.m_Multiplicity = value.toHash().count(); } else { item.m_Multiplicity = 1; } item.m_Name = qmp.name(); item.m_Value = value; list->append(item); } mafMementoPropertyItem itemTagList; itemTagList.m_Multiplicity = obj->tagList()->count(); itemTagList.m_Name = "TagList"; itemTagList.m_Value = *obj->tagList(); list->append(itemTagList); mafMementoPropertyItem itemScriptList; itemScriptList.m_Multiplicity = obj->scriptList()->count(); itemScriptList.m_Name = "ScriptList"; itemScriptList.m_Value = *obj->scriptList(); list->append(itemScriptList); mafMementoPropertyItem itemDic; itemDic.m_Multiplicity = obj->dictionary()->count(); itemDic.m_Name = "dictionary"; itemDic.m_Value = *(obj->dictionary()); list->append(itemDic); }
QString QcepObject::settingsScript() { QString res = ""; const QMetaObject* metaObject = this->metaObject(); for (int i=1; i < metaObject->propertyCount(); i++) { QMetaProperty prop = metaObject->property(i); if (prop.isStored()) { res += tr("%1.%2 = %3;\n").arg(this->get_Name()) .arg(prop.name()) .arg(scriptValueLiteral(this->property(prop.name()))); } } return res; }
void PropertyItemFromQObject::importPropertyForClass( const QObject *object, const QMetaObject *metaobject, PropertyItem*parent ) { if ( !metaobject ) return ; importPropertyForClass( object, metaobject->superClass(), parent ); PropertyItem *current = new PropertyItemSection( metaobject->className(), parent ); for ( int i = 0;i < metaobject->propertyCount();i++ ) { QMetaProperty qmprop = metaobject->property( i ); //on passe les proprietes aussi presentes dans la classe mere if ( metaobject->superClass() != 0 && metaobject->superClass() ->indexOfProperty( qmprop.name() ) > -1 ) continue; //on passe aussi les non storable -> je comprends pas comment on s'en sert if ( !qmprop.isStored() ) continue; buildPropertyItem( object, qmprop, current ); } }
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(); }
ObjectMapperForwarder::ObjectMapperForwarder(ObjectMapper *m, QObject *o) : QObject(m), m_mapper(m), m_source(o) { for (int i = 0; i < o->metaObject()->propertyCount(); ++i) { QMetaProperty property = o->metaObject()->property(i); if (!property.isStored() || !property.isReadable() || !property.isWritable()) { continue; } if (!property.hasNotifySignal()) { DEBUG << "ObjectMapperForwarder: No notify signal for property " << property.name() << endl; continue; } // Signals can be connected to slots with fewer arguments, so // long as the arguments they do have match. So we connect // the property notify signal to our universal // property-changed slot, and use the sender() of that to // discover which object's property has changed. // Unfortunately, we don't then know which property it was // that changed, as the identity of the signal that activated // the slot is not available to us. The annoying part of this // is that Qt does actually store the identity of the signal // in the same structure as used for the sender() object data; // it just doesn't (at least as of Qt 4.5) make it available // through the public API. QString sig = QString("%1%2").arg(QSIGNAL_CODE) .arg(property.notifySignal().methodSignature().data()); QByteArray ba = sig.toLocal8Bit(); if (!connect(o, ba.data(), this, SLOT(objectModified()))) { std::cerr << "ObjectMapperForwarder: Failed to connect notify signal" << std::endl; } } connect(o, SIGNAL(destroyed(QObject *)), this, SLOT(objectDestroyed())); }
QString ObjectStaticPropertyModel::detailString(const QMetaProperty& prop) const { QStringList s; s << tr("Constant: %1").arg(translateBool(prop.isConstant())); s << tr("Designable: %1").arg(translateBool(prop.isDesignable(m_obj.data()))); s << tr("Final: %1").arg(translateBool(prop.isFinal())); if (prop.hasNotifySignal()) { s << tr("Notification: %1").arg(Util::prettyMethodSignature(prop.notifySignal())); } else { s << tr("Notification: no"); } s << tr("Resetable: %1").arg(translateBool(prop.isResettable())); #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) s << tr("Revision: %1").arg(prop.revision()); #endif s << tr("Scriptable: %1").arg(translateBool(prop.isScriptable(m_obj.data()))); s << tr("Stored: %1").arg(translateBool(prop.isStored(m_obj.data()))); s << tr("User: %1").arg(translateBool(prop.isUser(m_obj.data()))); s << tr("Writable: %1").arg(translateBool(prop.isWritable())); return s.join("\n"); }
QString QMetaPropertyAdaptor::detailString(const QMetaProperty &prop) const { QObject *obj = object().qtObject(); QStringList s; s << tr("Constant: %1").arg(translateBool(prop.isConstant())); s << tr("Designable: %1").arg(translateBool(prop.isDesignable(obj))); s << tr("Final: %1").arg(translateBool(prop.isFinal())); if (prop.hasNotifySignal()) s << tr("Notification: %1").arg(Util::prettyMethodSignature(prop.notifySignal())); else s << tr("Notification: no"); s << tr("Resetable: %1").arg(translateBool(prop.isResettable())); #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) s << tr("Revision: %1").arg(prop.revision()); #endif s << tr("Scriptable: %1").arg(translateBool(prop.isScriptable(obj))); s << tr("Stored: %1").arg(translateBool(prop.isStored(obj))); s << tr("User: %1").arg(translateBool(prop.isUser(obj))); s << tr("Writable: %1").arg(translateBool(prop.isWritable())); return s.join(QStringLiteral("\n")); }
void tst_QQmlMetaObject::property() { QFETCH(QString, testFile); QFETCH(QByteArray, cppTypeName); QFETCH(int, cppType); QFETCH(bool, isDefault); QFETCH(QVariant, expectedValue); QFETCH(bool, isWritable); QFETCH(QVariant, newValue); QQmlEngine engine; QQmlComponent component(&engine, testFileUrl(testFile)); QObject *object = component.create(); QVERIFY(object != 0); const QMetaObject *mo = object->metaObject(); QVERIFY(mo->superClass() != 0); QVERIFY(QByteArray(mo->className()).contains("_QML_")); QCOMPARE(mo->propertyOffset(), mo->superClass()->propertyCount()); QCOMPARE(mo->propertyCount(), mo->superClass()->propertyCount() + 1); QMetaProperty prop = mo->property(mo->propertyOffset()); QCOMPARE(prop.name(), "test"); QCOMPARE(QByteArray(prop.typeName()), cppTypeName); if (prop.userType() < QMetaType::User) QCOMPARE(prop.type(), QVariant::Type(cppType)); else QCOMPARE(prop.type(), QVariant::UserType); QCOMPARE(prop.userType(), cppType); QVERIFY(!prop.isConstant()); QVERIFY(!prop.isDesignable()); QVERIFY(!prop.isEnumType()); QVERIFY(!prop.isFinal()); QVERIFY(!prop.isFlagType()); QVERIFY(prop.isReadable()); QVERIFY(!prop.isResettable()); QVERIFY(prop.isScriptable()); QVERIFY(!prop.isStored()); QVERIFY(!prop.isUser()); QVERIFY(prop.isValid()); QCOMPARE(prop.isWritable(), isWritable); QCOMPARE(mo->classInfoOffset(), mo->superClass()->classInfoCount()); QCOMPARE(mo->classInfoCount(), mo->superClass()->classInfoCount() + (isDefault ? 1 : 0)); if (isDefault) { QMetaClassInfo info = mo->classInfo(mo->classInfoOffset()); QCOMPARE(info.name(), "DefaultProperty"); QCOMPARE(info.value(), "test"); } QCOMPARE(mo->methodOffset(), mo->superClass()->methodCount()); QCOMPARE(mo->methodCount(), mo->superClass()->methodCount() + 1); // the signal QVERIFY(prop.notifySignalIndex() != -1); QMetaMethod signal = prop.notifySignal(); QCOMPARE(signal.methodType(), QMetaMethod::Signal); QCOMPARE(signal.name(), QByteArray("testChanged")); QCOMPARE(signal.methodSignature(), QByteArray("testChanged()")); QCOMPARE(signal.access(), QMetaMethod::Public); QCOMPARE(signal.parameterCount(), 0); QCOMPARE(signal.parameterTypes(), QList<QByteArray>()); QCOMPARE(signal.parameterNames(), QList<QByteArray>()); QCOMPARE(signal.tag(), ""); QCOMPARE(signal.typeName(), "void"); QCOMPARE(signal.returnType(), int(QMetaType::Void)); QSignalSpy changedSpy(object, SIGNAL(testChanged())); QObject::connect(object, SIGNAL(testChanged()), object, SLOT(deleteLater())); if (expectedValue.isValid()) QCOMPARE(prop.read(object), expectedValue); else QVERIFY(prop.read(object).isValid()); QCOMPARE(changedSpy.count(), 0); if (isWritable) { QVERIFY(prop.write(object, newValue)); QCOMPARE(changedSpy.count(), 1); QCOMPARE(prop.read(object), newValue); } else { QVERIFY(!prop.write(object, prop.read(object))); QCOMPARE(changedSpy.count(), 0); } delete object; }