Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QML Signal and Handler Event System

Application and user interface components communicate with each other. For example, a button component needs to know that the user is clicking on it. The button may change colors to indicate its state or perform some logic. As well, application needs to know whether the user is clicking the button. The application may need to relay this clicking event to other applications.

QML has a signal and handler mechanism, where the signal is the event and the component responds to the event through the handler. The signal is emitted and the handler is invoked. Placing logic such as scripts or other operations in the handler allows the component to respond to the event.

Signals and Handlers

Signals provide a way to notify other objects when an event has occurred. For example, the MouseArea clicked signal notifies other objects that the mouse has been clicked within the area.

The syntax for defining a new signal is:

signal <name>[([<type> <parameter name>[, ...]])]

Attempting to declare two signals or methods with the same name in the same type block generates an error. However, a new signal may reuse the name of an existing signal on the type. (This should be done with caution, as the existing signal may be hidden and become inaccessible.)

Here are various examples of signal declarations:


If the signal has no parameters, the "()" brackets are optional. If parameters are used, the parameter types must be declared, as for the string and variant arguments of the perform signal.

Adding a signal to an item automatically adds a signal handler as well. The signal hander is named on<SignalName>, with the first letter of the signal in uppercase. The previous signals have the following signal handlers:


Further, each QML properties have a <property_name>Changed signal and its corresponding on<property_name>Changed signal handler. As a result, property changes may notify other components for any changes.


To emit a signal, invoke it as a method. The signal handler binding is similar to a property binding and it is invoked when the signal is emitted. Use the defined argument names to access the respective arguments.


Note that the Component.onCompleted is an attached signal handler; it is invoked when the Component initialization is complete.

Connecting Signals to Methods and Signals

Signal objects have a connect() method to a connect a signal either to a method or another signal. When a signal is connected to a method, the method is automatically invoked whenever the signal is emitted. This mechanism enables a signal to be received by a method instead of a signal handler.


The connect() method is appropriate when connecting a JavaScript method to a signal.

There is a corresponding disconnect() method for removing connected signals.

Signal to Signal Connect

By connecting signals to other signals, the connect() method can form different signal chains.


Whenever the MouseArea clicked signal is emitted, the send signal will automatically be emitted as well.

 output:
     MouseArea clicked
     Send clicked

Events from the Declarative Runtime

There maybe cases where a signal comes from the declarative runtime. For example, it is possible to receive events from QML plugins. For more signal control, the connect() method and the Connections element may connect a signal from the runtime to another signal or method.

For complete information on events from the runtime or creating signals from the runtime, read the The QML Engine and the C++ Types as QML Types articles.

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web