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  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

Importing Reusable Components

A component is an instantiable QML definition, typically contained in a .qml file. For instance, a Button component may be defined in Button.qml. The QML runtime may instantiate this Button component to create Button objects. Alternatively, a component may be defined inside a Component element.

Moreover, the Button definition may also contain other components. A Button component could use a Text element for its label and other components to implement its functions. Compounding components to form new components (and effectively new interfaces) is the emphasis in QML.

Defining New Components

Any snippet of QML code may become a component, by placing the code in a QML file (extension is .qml). A complete Button component that responds to user input may be in a Button.qml file.

 //contents of Button.qml
 import QtQuick 1.0

 Rectangle {
     id: button
     width: 145; height: 60
     color: "blue"
     smooth: true; radius: 9
     property alias text: label.text
     border {color: "#B9C5D0"; width: 1}

     gradient: Gradient {
         GradientStop {color: "#CFF7FF"; position: 0.0}
         GradientStop {color: "#99C0E5"; position: 0.57}
         GradientStop {color: "#719FCB"; position: 0.9}
     }

     Text {
         id: label
         anchors.centerIn: parent
         text: "Click Me!"
         font.pointSize: 12
         color: "blue"
     }

     MouseArea {
         anchors.fill: parent
         onClicked: console.log(text + " clicked")
     }
 }

Alternatively, a Component element may encapsulate a QML object to form a component.

 Rectangle {
     Component {
         id: inlinecomponent
         Rectangle {
             id: display
             width: 50; height: 50
             color: "blue"
         }
     }
 }

Loading a Component

The initialization of inline components is different from loading a component from a .qml file.

Importing a Component

A component defined in a .qml file is directly usable by declaring the name of the component. For example, a button defined in Button.qml is created by declaring a Button. The button is defined in the Defining New Components section.

 import QtQuick 1.0

 Rectangle {
     width: 175; height: 350
     color: "lightgrey"

     Column {
         anchors.centerIn: parent
         spacing: 15
         Button {}
         Button {text: "Me Too!"}
         Button {text: "Me Three!"}
     }
 }

Note that the component name, Button, matches the QML filename, Button.qml. Also, the first character is in upper case. Matching the names allow components in the same directory to be in the direct import path of the application.

For flexibility, a qmldir file is for dictating which additional components, plugins, or directories should be imported. By using a qmldir file, component names do not need to match the filenames. The qmldir file should, however, be in an imported path.

 Button ./Button.qml
 FocusButton ./focusbutton.qml

Loading an Inline Component

A consequence of inline components is that initialization may be deferred or delayed. A component may be created during a MouseArea event or by using a Loader element. The component can create an object, which is addressable in a similar way as an id property. Thus, the created object may have its bindings set and read like a normal QML object.

     Component {
         id: inlinecomponent
         Rectangle {
             id: display
             width: 50; height: 50
             color: "blue"
         }
     }
     MouseArea {
         anchors.fill: parent
         onClicked: {
             inlinecomponent.createObject(parent)

             var second = inlinecomponent.createObject(parent)

             var third = inlinecomponent.createObject(parent)
             third.x = second.width + 10
             third.color = "red"
         }
     }

Component Properties

Initializing a component, either from a .qml file or initializing an inline component, have several properties to facilitate component execution. Specifically, there are attached properties and attached signal handlers for setting properties during the lifetime of a component.

The Component.onCompleted attached signal handler is called when the component completes initialization. It is useful for executing any commands after component initialization. Similarly, the Component.onDestruction signal handler executes when the component finishes destruction.

Top-Level Component

Choosing the top-level or the root object of components is an important design aspect because the top-level object dictates which properties are accessible outside the component. Some elements are not visual elements and will not have visual properties exposed outside the component. Likewise, some elements add functionality that are not available to visual elements.

Consider the Button component from the Defining New Components section; it's top-level object is a Rectangle. When imported, the Button component will possess the Rectangle's properties, methods, signals, and any custom properties.

 Rectangle {
     //...
     width: 145; height: 60
     color: "blue"
     smooth: true; radius: 9
     property alias text: label.text
     //...
 }

The Button's text alias is accessible from outside the component as well as the Rectangle's visual properties and signals such as x, y, anchors, and states.

Alternatively, we may choose a FocusScope as our top-level object. The FocusScope element manage keyboard focus for its children which is beneficial for certain types of interfaces. However, since FocusScopes are not visual elements, the visual properties of its child need to be exposed.

 //contents of focusbutton.qml
 import QtQuick 1.0

 FocusScope {

     //FocusScope needs to bind to visual properties of the children
     property alias color: button.color
     x: button.x; y: button.y
     width: button.width; height: button.height

     Rectangle {
         id: button
         width: 145; height: 60
         color: "blue"
         smooth: true; radius: 9
         property alias text: label.text
         border {color: "#B9C5D0"; width: 1}

         gradient: Gradient {
             GradientStop {color: "#CFF7FF"; position: 0.0}
             GradientStop {color: "#99C0E5"; position: 0.57}
             GradientStop {color: "#719FCB"; position: 0.9}
         }

         Text {
             id: label
             anchors.centerIn: parent
             text: "Click Me!"
             font.pointSize: 12
             color: "blue"
         }

         MouseArea {
             anchors.fill: parent
             onClicked: console.log(text + " clicked")
         }
     }
 }
Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le blog Digia au hasard

Logo

Créer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut Cuvelier

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

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.7-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