Accessing Qt methods, members, properties, signals, slots and enums (Qt)

Last edited on

Standard Qt classes

Squish test scripts can access methods and members of standard Qt classes.

Custom Qt classes

For custom Qt classes, their methods are accessible in test scripts if they have been declared as Qt public slots or declared public and marked with Q_INVOKABLE.

In addition, public properties that have been declared as QObject properties via Q_PROPERTY, are available for both verifications and search by name.

Using properties for object name generation

Properties declared via Q_PROPERTY can be configured and used by the Squish name generator.

Dynamic properties set via QObject.setProperty() can be queried via QObject.property() but are unfortunately not available as properties and automatic name generation.

(Also see Object Name Generation .)

Custom enums

In addition, the Q_ENUMS macro can be used to declare custom enum types to the (Qt) "moc" tool, which should make them accessible from Squish.

Properties returning QVariant

Some Properties in Qt Quick return their value wrapped in QVariants. This is for example most common in the case of the model, for example for listviews. The QVariant itself can be converted to the base class as shown in the example below

listview = waitForObject(...)
qvariant = listview.model
realmodel = object.convertTo(qvariant,qvariant.typeName())
QVariant

The realmodel variable contains the original value if the conversion was successful.

Properties returning QVariantMap

Assuming the object myObject has a property of type QVariantMap accessing its contents works almost the same way as in Qt. It is only necessary to manually convert the returned QVariant objects to their actual value types. That means calling QVariant::toMap() for example. This is done through Squish's object.convertTo(obj, typename) function.

The following code example retrieves a QVariantMap property from myObject and prints all the keys of the map. It works for QVariantMaps where the values are of type QVariant<QString>. For different value types, the conversion of valueVariant needs to be adjusted.

myVariantMap = object.convertTo(myObject.variantMap, "QVariantMap")
keysVariant = myVariantMap.keys()
keys = object.convertTo(keysVariant, "QList<QString>")

for i in range(keys.size()):
    keyVariant = keys.at(i)
    key = object.convertTo(keyVariant, "QString")
    valueVariant = myVariantMap.value(key)
    value = object.convertTo(valueVariant, "QString")
    test.log("%s: %s" % (key, value))

}
QVariantMap Example

Limitations

Please note that template based return types are not supported and the respective property or slot will not be listed in the Properties or Methods views in the Squish IDE.

A possible workaround can be to return an object based on a non template based class.