object.convertTo

Object object.convertTo(object, typeName)

This function is for converting value types, and (in Squish for Qt) getting values out of a QVariant.

This function converts the given object to an object of the type specified by the typeName string. (A suitable object can be obtained using the Object waitForObject(objectOrName) function or the Object findObject(objectName) function.) For example:

font = object.convertTo(variant, "QFont")

In this example, if variant is a variable of type QVariant and it holds a QFont object, the underlying QFont object will be returned. If the object is invalid or null, if the typeName is unrecognized, or if the conversion isn't possible (e.g., QFont to int), a catchable exception will be raised.

This function can also do other kinds of conversions, e.g., int to String. See also, the Object cast(object, type) function which can be used for downcasting (which object.convertTo cannot do).

Note: In Squish for Qt, you can extract pointers to QObject-derived classes from QVariant and cast them to the correct type if the class is registered with QMetaType. The magic is to not use object.convertTo(myVariant, "CustomClassName") as the above example suggests (which is valid for other cases), but instead to use object.convertTo(myVariant, "QObject").

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.
The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation.
Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.

Search Results