Custom Type Sending ExampleFiles:
The Custom Type Sending example shows how to use a custom type with signals and slots. In the Custom Type Example, we showed how to integrate custom types with the meta-object system, enabling them to be stored in QVariant objects, written out in debugging information and used in signal-slot communication. In this example, we demonstrate that the preparations made to the Message class and its declaration with Q_DECLARE_METATYPE() enable it to be used with direct signal-slot connections. We do this by creating a Window class containing signals and slots whose signatures include Message arguments. We define a simple Window class with a signal and public slot that allow a Message object to be sent via a signal-slot connection: The window will contain a text editor to show the contents of a message and a push button that the user can click to send a message. To facilitate this, we also define the sendMessage() slot. We also keep a Message instance in the thisMessage private variable which holds the actual message to be sent. The Message class is defined in the following way: The type is declared to the meta-type system with the Q_DECLARE_METATYPE() macro: This will make the type available for use in direct signal-slot connections. The Window constructor sets up a user interface containing a text editor and a push button. The button's clicked() signal is connected to the window's sendMessage() slot, which emits the messageSent(Message) signal with the Message held by the thisMessage variable: We implement a slot to allow the message to be received, and this also lets us set the message in the window programatically: In this function, we simply assign the new message to thisMessage and update the text in the editor. In the example's main() function, we perform the connection between two instances of the Window class: We set the message for the first window and connect the messageSent(Message) signal from each window to the other's setMessage(Message) slot. Since the signals and slots mechanism is only concerned with the type, we can simplify the signatures of both the signal and slot when we make the connection. When the user clicks on the Send message button in either window, the message shown will be emitted in a signal that the other window will receive and display. Although the custom Message type can be used with direct signals and slots, an additional registration step needs to be performed if you want to use it with queued signal-slot connections. See the Queued Custom Type Example for details. More information on using custom types with Qt can be found in the Creating Custom Qt Types document. |
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 4.8 | |
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 ! |
Copyright © 2000-2012 - www.developpez.com