Log in / Register
Home arrow Computer Science arrow Learn BlackBerry 10 App Development
< Prev   CONTENTS   Next >

QObject::connect( )

The bool QObject::connect(const QObject* sender, const char* signal, const QObject* receiver, const char* slot, ConnectionType = AutoConnection) method connects a sender's signal to the receiver's slot. As you can see, the signal and slot parameters are C strings. You will therefore have to use the corresponding SIGNAL() and SLOT() macros in order to convert

function signatures into strings. Behind the scenes, QObject::connect() compares the strings with introspection data generated by the MOC tool. Here is a simple example illustrating how to use the connect method:

QObject::connect(sender, SIGNAL(valueChanged(int)), receiver, SLOT(setValue(int)).

Note that the connect method returns a bool value that you should always check to make sure that the connection was successful. During development, a best practice is to pass QObject::connect()'s return value to the Q_ASSERT(bool test) macro (the macro is enabled in debug builds; prints a warning message if the test fails and halts program execution). In practice, you should never ignore a failed connection because your application might behave erratically or crash in release versions.

As you might have guessed, the QObject::connect() mechanism happens at runtime without any type checking during the compilation process. In practice, this can be quite frustrating when you have to debug silently failing connections. As a general rule of thumb, if a QObject::connect() fails, check the following points:

n Make sure that the signal and slot parameter types correspond. A slot can take fewer parameters than an emitting signal and the extra parameters will be dropped; however, it is essential that the parameter types match.

n If a parameter type is defined in a namespace, make sure to use the fully qualified type name by including the namespace (see Listing 3-13).

Listing 3-13. QObject::connect( )


SIGNAL(imageChanged(bb::cascades::Image*)), myHandler, SLOT(onImageChanged(bb::cascades::Image*)));

Finally, you can also disconnect a signal from a slot using QObject::disconnect(const QObject* sender, const char* signal, const QObject* receiver, const char* slot).

QObject::setProperty( )

You can update QObject properties defined with the Q_PROPERTY() macro using the QObject::setProperty(const char* propertyname, const QVariant& value) method. A QVariant is a union of common Qt data types; however, at any time the QVariant can contain a single variable of a given type. If the property was not defined with the Q_PROPERTY() macro, QObject::setProperty() will create a new dynamic property and add it to the QObject instance. Similarly, you can get a property's value using QVariant QObject::property(const char* propertyname). As you will see later in this chapter, properties are a fundamental aspect of exchanging data between C++ and QML by using bindings (a binding can update a Cascades control's property when a corresponding C++ property changes or vice-versa, depending on the binding target).

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science