Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
Sonde::ObjectTree Namespace Reference

Process-wide view of the application's object tree, used by the introspection tools and by tools that target objects (clicks, screenshots). More...

Typedefs

using RootsProvider = std::function<QObjectList()>
 Returns additional root objects.
using ChildrenProvider = std::function<std::optional<QObjectList>(QObject *)>
 Returns the children of an object, or std::nullopt to fall through to the next provider and finally QObject::children().
using InspectProvider = std::function<QStringList(const QObject *)>
 Returns extra "name: value" lines object_inspect shows for an object (e.g.

Functions

void addRootsProvider (const QString &key, RootsProvider provider)
 Registers (or, for an existing key, replaces) a roots provider.
void addChildrenProvider (const QString &key, ChildrenProvider provider)
 Registers (or, for an existing key, replaces) a children provider.
void addInspectProvider (const QString &key, InspectProvider provider)
 Registers (or, for an existing key, replaces) an inspect provider.
QStringList inspectLines (const QObject *object)
 The extra lines every inspect provider gives for object.
QObjectList roots ()
 All roots, without duplicates; always includes the application object.
QObjectList children (QObject *object)
 The children of object according to the providers.
QString idFor (const QObject *object)
 The id of object ("@0x..."), accepted by find().
template<typename Visit>
static bool walk (const QObjectList &start, Visit visit)
 Breadth-first walk from start; visit returns true to stop.
bool contains (const QObject *object)
 Whether object is reachable from the roots (and therefore safe to use).
bool isOfClass (const QObject *object, QStringView className)
 Whether object is of class className or inherits it.
Expected< QObject * > find (const QString &spec)
 Resolves an object spec (see the namespace documentation).
QString describeBrief (const QObject *object)
 Short summary that doesn't change with the object's state: @id Class#name.
QString describe (const QObject *object)
 One-line summary: @id Class#name text="..." (hidden) (disabled).
QJsonValue variantToJson (const QVariant &value)
 Converts a QVariant to JSON.
Expected< QVariant > jsonToVariant (const QJsonValue &json, QMetaType type)
 Converts JSON to a QVariant of type type (the inverse of variantToJson()).

Detailed Description

Process-wide view of the application's object tree, used by the introspection tools and by tools that target objects (clicks, screenshots).

The tree starts at a set of roots (by default the QCoreApplication) and descends through QObject::children(). Integrations (Sonde::Gui, Widgets, Quick) add roots such as top-level windows, and replace the children of objects whose visual children differ from their QObject children (e.g. QQuickItem::childItems()).

Object specs, as accepted by find():

Spec Meaning
@0x7f8e4c00a0 an id previously returned by idFor() or the tools
okButton first object whose objectName or class is okButton
QPushButton#ok class (inherits) and objectName
#ok objectName only
QPushButton[text=OK] class plus property value (also [text=OK])
mainWindow/QPushButton[text=Save] path; each segment is searched below the previous match

Not thread-safe; use from the GUI thread.

Typedef Documentation

◆ InspectProvider

using Sonde::ObjectTree::InspectProvider = std::function<QStringList(const QObject *)>

Returns extra "name: value" lines object_inspect shows for an object (e.g.

the QML file it was declared in), or none.

Function Documentation

◆ find()

Expected< QObject * > Sonde::ObjectTree::find ( const QString & spec)

Resolves an object spec (see the namespace documentation).

Ids are only accepted for objects currently reachable from the roots, so a stale id never dereferences a dangling pointer.

Returns
The object, or a failure that says why nothing matched.

◆ isOfClass()

bool Sonde::ObjectTree::isOfClass ( const QObject * object,
QStringView className )

Whether object is of class className or inherits it.

Like QObject::inherits(), but a QML type also matches by the name it has in QML: Qt names the class of a type written in QML ControlPanel_QMLTYPE_11 (or ControlPanel_QML_3), with a number that changes from run to run, and "ControlPanel" matches it.

◆ jsonToVariant()

Expected< QVariant > Sonde::ObjectTree::jsonToVariant ( const QJsonValue & json,
QMetaType type )

Converts JSON to a QVariant of type type (the inverse of variantToJson()).

A gadget is built from an object: its default value, with each key written to the property of that name. Read-only properties are skipped, and a key that names no property fails.

Parameters
jsonThe value to convert.
typeThe target meta type.
Returns
The converted value, or a failure that says why it couldn't be converted.

◆ variantToJson()

QJsonValue Sonde::ObjectTree::variantToJson ( const QVariant & value)

Converts a QVariant to JSON.

Handles geometry types ({x, y, width, height}), URLs, byte arrays, enums and flags (as key names), QObject pointers (as ids), gadgets (Q_GADGET types such as QGeoCoordinate, as an object of their readable properties), sequential and associative containers (as arrays and objects, each element converted by these rules) and everything QJsonValue::fromVariant() supports; other types fall back to their string form or "<TypeName>". Containers and gadgets nested more than 16 deep also become "<TypeName>", so a value that contains itself ends.

◆ walk()

template<typename Visit>
bool Sonde::ObjectTree::walk ( const QObjectList & start,
Visit visit )
static

Breadth-first walk from start; visit returns true to stop.

Returns
Whether it stopped early.

Breadth-first walk; visit returns true to stop.