void PQBaseItem::serializeObject(QTextStream &stream, const QObject *object, unsigned indentSize) const { QString value; const QMetaObject *objectInfo; if (object == 0) { return; // empty string } objectInfo = object->metaObject(); // Handle object name QString objectName = QString::fromLatin1(objectInfo->className()); // Some QML objects instantiates directly C++ classes, the name must be stripedd of Q* prefix static QStringList unwantedPrefixes = QStringList() << QLatin1String("QGraphics") << QLatin1String("QDeclarative"); Q_FOREACH (const QString &prefix, unwantedPrefixes) { if (objectName.startsWith(prefix)) { objectName.remove(0, prefix.length()); } } stream << PQSerializer::indentStep().repeated(indentSize) << objectName << QLatin1String(" {") << endl; for (int i = objectInfo->propertyOffset(); i < objectInfo->propertyCount(); ++i) { serializeProperty(stream, QDeclarativeProperty(const_cast<QObject *>(object), QString::fromLatin1(objectInfo->property(i).name())), indentSize + 1); } stream << PQSerializer::indentStep().repeated(indentSize) << QLatin1String("}"); // close object }
//! Class constructor Layer::Layer(QQuickItem *parent) : QQuickPaintedItem(parent) , m_drawType(Quasi::TiledDrawType) , m_factor(1.0) , m_type(Quasi::InfiniteType) , m_direction((Quasi::LayerDirection)-1) // Backward , m_areaToDraw(2.0) , m_columnOffset(0) , m_drawingMirrored(false) , m_shouldMirror(false) , m_tileWidth(32) , m_tileHeight(32) , m_latestPoint(0) { setZ(Quasi::InteractionLayerOrdering_01); // this activates the item layered mode QDeclarativeProperty(this, "layer.enabled").write(true); }
int main(int argc, char *argv[]) { QApplication app(argc, argv); //![QDeclarativeView] // Using QDeclarativeView QDeclarativeView view; view.setSource(QUrl::fromLocalFile("MyItem.qml")); view.show(); QObject *object = view.rootObject(); //![QDeclarativeView] //![properties] object->setProperty("width", 500); QDeclarativeProperty(object, "width").write(500); //![properties] //![cast] QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(object); item->setWidth(500); //![cast] //![findChild] QObject *rect = object->findChild<QObject*>("rect"); if (rect) rect->setProperty("color", "red"); //![findChild] //![QDeclarativeComponent-b] delete object; //![QDeclarativeComponent-b] withComponent(); return app.exec(); }
void tst_QDeclarativeDebug::recursiveObjectTest(QObject *o, const QDeclarativeDebugObjectReference &oref, bool recursive) const { const QMetaObject *meta = o->metaObject(); QDeclarativeType *type = QDeclarativeMetaType::qmlType(meta); QString className = type ? QString(type->qmlTypeName()) : QString(meta->className()); className = className.mid(className.lastIndexOf(QLatin1Char('/'))+1); QCOMPARE(oref.debugId(), QDeclarativeDebugService::idForObject(o)); QCOMPARE(oref.name(), o->objectName()); QCOMPARE(oref.className(), className); QCOMPARE(oref.contextDebugId(), QDeclarativeDebugService::idForObject(qmlContext(o))); const QObjectList &children = o->children(); for (int i=0; i<children.count(); i++) { QObject *child = children[i]; if (!qmlContext(child)) continue; int debugId = QDeclarativeDebugService::idForObject(child); QVERIFY(debugId >= 0); QDeclarativeDebugObjectReference cref; foreach (const QDeclarativeDebugObjectReference &ref, oref.children()) { if (ref.debugId() == debugId) { cref = ref; break; } } QVERIFY(cref.debugId() >= 0); if (recursive) recursiveObjectTest(child, cref, true); } foreach (const QDeclarativeDebugPropertyReference &p, oref.properties()) { QCOMPARE(p.objectDebugId(), QDeclarativeDebugService::idForObject(o)); // signal properties are fake - they are generated from QDeclarativeBoundSignal children if (p.name().startsWith("on") && p.name().length() > 2 && p.name()[2].isUpper()) { QVERIFY(p.value().toString().startsWith('{') && p.value().toString().endsWith('}')); QVERIFY(p.valueTypeName().isEmpty()); QVERIFY(p.binding().isEmpty()); QVERIFY(!p.hasNotifySignal()); continue; } QMetaProperty pmeta = meta->property(meta->indexOfProperty(p.name().toUtf8().constData())); QCOMPARE(p.name(), QString::fromUtf8(pmeta.name())); if (pmeta.type() > 0 && pmeta.type() < QVariant::UserType) // TODO test complex types QCOMPARE(p.value(), pmeta.read(o)); if (p.name() == "parent") QVERIFY(p.valueTypeName() == "QGraphicsObject*" || p.valueTypeName() == "QDeclarativeItem*"); else QCOMPARE(p.valueTypeName(), QString::fromUtf8(pmeta.typeName())); QDeclarativeAbstractBinding *binding = QDeclarativePropertyPrivate::binding(QDeclarativeProperty(o, p.name())); if (binding) QCOMPARE(binding->expression(), p.binding()); QCOMPARE(p.hasNotifySignal(), pmeta.hasNotifySignal()); QVERIFY(pmeta.isValid()); } }