PropertiesQt provides a sophisticated property system similar to those shipped by some compiler vendors. However, as a compiler- and platform-independent library, Qt cannot rely on non-standard compiler features like __property or [property]. Our solution works with
any standard C++ compiler on every platform we support. It's based on
the Meta Object System that also
provides object communication through signals and slots.
The To the outer world, a property appears quite similar to a data member. However, properties have several features that distinguish them from ordinary data members:
Properties can be read and written through generic functions in QObject without knowing anything about class in use. These two are equivalent:
//QButton * b and QObject * o point to the same button b->setDown( TRUE ); o->setProperty( "down", TRUE ); Equivalent, that is, except that the first is faster, and that the first gives much better diagnostics at compile time. When accessible, the first is preferred. However, since you can get a list of all available properties for any QObject through its metaObject() setProperty() can give you control over classes that weren't available at compile time. As well as QObject::setProperty(), there is a QObject::property(). QMetaObject::propertyNames() returns the names of all available properties. QMetaObject::property() returns the property data for a named property: a QMetaProperty object. Here's a simple example that shows the most important property functions in use:
class MyClass : public QObject { Q_OBJECT public: MyClass( QObject * parent=0, const char * name=0 ); ~MyClass(); enum Priority { High, Low, VeryHigh, VeryLow }; void setPriority( Priority ); Priority priority() const; };
The class has a property "priority" that is not yet known to the meta
object system. In order to make the property known, you have to
declare it with the
Q_PROPERTY( type name READ getFunction [WRITE setFunction] [STORED bool] [DESIGNABLE bool] [RESET resetFunction]) For the declaration to be valid, the get function has to be const and to return either the type itself, a pointer to it, or a reference to it. The optional write function has to return void and to take exactly one argument, either the type itself, a pointer or a const reference to it. The meta object compiler enforces this.
The type of a property can be everything QVariant provides or an
enumeration type declared in the class itself. Since
obj->setProperty( "priority", "VeryHigh" );
Enumeration types are registered with the
class MyClass : public QObject { Q_OBJECT Q_PROPERTY( Priority priority READ priority WRITE setPriority ) Q_ENUMS( Priority ) public: MyClass( QObject * parent=0, const char * name=0 ); ~MyClass(); enum Priority { High, Low, VeryHigh, VeryLow }; void setPriority( Priority ); Priority priority() const; };
Another similar macro is
The remaining keywords in the
|
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
![]()
![]() Le Qt Quarterly au hasard![]() Le tri des QListViewQt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. Lire l'article.
CommunautéRessources
Liens utilesContact
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 2.3 | |
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