Notice: Undefined offset: 1 in /home/developpez/www/developpez-com/upload/qt/doc/redir.php on line 39 Qt 4.7: QFlags Class Reference
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  · 

QFlags Class Reference

The QFlags class provides a type-safe way of storing OR-combinations of enum values. More...

 #include <QFlags>

Public Types

typedef enum_type

Public Functions

QFlags ( const QFlags & other )
QFlags ( Enum flag )
QFlags ( Zero zero = 0 )
QFlags ( QFlag value )
bool testFlag ( Enum flag ) const
operator int () const
bool operator! () const
QFlags operator& ( int mask ) const
QFlags operator& ( uint mask ) const
QFlags operator& ( Enum mask ) const
QFlags & operator&= ( int mask )
QFlags & operator&= ( uint mask )
QFlags & operator= ( const QFlags & other )
QFlags operator^ ( QFlags other ) const
QFlags operator^ ( Enum other ) const
QFlags & operator^= ( QFlags other )
QFlags & operator^= ( Enum other )
QFlags operator| ( QFlags other ) const
QFlags operator| ( Enum other ) const
QFlags & operator|= ( QFlags other )
QFlags & operator|= ( Enum other )
QFlags operator~ () const

Macros

Q_DECLARE_FLAGS ( Flags, Enum )
Q_DECLARE_OPERATORS_FOR_FLAGS ( Flags )

Detailed Description

The QFlags class provides a type-safe way of storing OR-combinations of enum values.

The QFlags<Enum> class is a template class, where Enum is an enum type. QFlags is used throughout Qt for storing combinations of enum values.

The traditional C++ approach for storing OR-combinations of enum values is to use an int or uint variable. The inconvenience with this approach is that there's no type checking at all; any enum value can be OR'd with any other enum value and passed on to a function that takes an int or uint.

Qt uses QFlags to provide type safety. For example, the Qt::Alignment type is simply a typedef for QFlags<Qt::AlignmentFlag>. QLabel::setAlignment() takes a Qt::Alignment parameter, which means that any combination of Qt::AlignmentFlag values,or 0, is legal:

 label->setAlignment(Qt::AlignLeft | Qt::AlignTop);

If you try to pass a value from another enum or just a plain integer other than 0, the compiler will report an error. If you need to cast integer values to flags in a untyped fashion, you can use the explicit QFlags constructor as cast operator.

If you want to use QFlags for your own enum types, use the Q_DECLARE_FLAGS() and Q_DECLARE_OPERATORS_FOR_FLAGS().

Example:

 class MyClass
 {
 public:
     enum Option {
         NoOptions = 0x0,
         ShowTabs = 0x1,
         ShowAll = 0x2,
         SqueezeBlank = 0x4
     };
     Q_DECLARE_FLAGS(Options, Option)
     ...
 };

 Q_DECLARE_OPERATORS_FOR_FLAGS(MyClass::Options)

You can then use the MyClass::Options type to store combinations of MyClass::Option values.

Flags and the Meta-Object System

The Q_DECLARE_FLAGS() macro does not expose the flags to the meta-object system, so they cannot be used by Qt Script or edited in Qt Designer. To make the flags available for these purposes, the Q_FLAGS() macro must be used:

 Q_FLAGS(Options)

Naming Convention

A sensible naming convention for enum types and associated QFlags types is to give a singular name to the enum type (e.g., Option) and a plural name to the QFlags type (e.g., Options). When a singular name is desired for the QFlags type (e.g., Alignment), you can use Flag as the suffix for the enum type (e.g., AlignmentFlag).

See also QFlag.

Member Type Documentation

typedef QFlags::enum_type

Typedef for the Enum template type.

Member Function Documentation

QFlags::QFlags ( const QFlags & other )

Constructs a copy of other.

QFlags::QFlags ( Enum flag )

Constructs a QFlags object storing the given flag.

QFlags::QFlags ( Zero zero = 0 )

Constructs a QFlags object with no flags set. zero must be a literal 0 value.

QFlags::QFlags ( QFlag value )

Constructs a QFlags object initialized with the given integer value.

The QFlag type is a helper type. By using it here instead of int, we effectively ensure that arbitrary enum values cannot be cast to a QFlags, whereas untyped enum values (i.e., int values) can.

bool QFlags::testFlag ( Enum flag ) const

Returns true if the flag is set, otherwise false.

This function was introduced in Qt 4.2.

QFlags::operator int () const

Returns the value stored in the QFlags object as an integer.

bool QFlags::operator! () const

Returns true if no flag is set (i.e., if the value stored by the QFlags object is 0); otherwise returns false.

QFlags QFlags::operator& ( int mask ) const

Returns a QFlags object containing the result of the bitwise AND operation on this object and mask.

See also operator&=(), operator|(), operator^(), and operator~().

QFlags QFlags::operator& ( uint mask ) const

This is an overloaded function.

QFlags QFlags::operator& ( Enum mask ) const

This is an overloaded function.

QFlags & QFlags::operator&= ( int mask )

Performs a bitwise AND operation with mask and stores the result in this QFlags object. Returns a reference to this object.

See also operator&(), operator|=(), and operator^=().

QFlags & QFlags::operator&= ( uint mask )

This is an overloaded function.

QFlags & QFlags::operator= ( const QFlags & other )

Assigns other to this object and returns a reference to this object.

QFlags QFlags::operator^ ( QFlags other ) const

Returns a QFlags object containing the result of the bitwise XOR operation on this object and other.

See also operator^=(), operator&(), operator|(), and operator~().

QFlags QFlags::operator^ ( Enum other ) const

This is an overloaded function.

QFlags & QFlags::operator^= ( QFlags other )

Performs a bitwise XOR operation with other and stores the result in this QFlags object. Returns a reference to this object.

See also operator^(), operator&=(), and operator|=().

QFlags & QFlags::operator^= ( Enum other )

This is an overloaded function.

QFlags QFlags::operator| ( QFlags other ) const

Returns a QFlags object containing the result of the bitwise OR operation on this object and other.

See also operator|=(), operator^(), operator&(), and operator~().

QFlags QFlags::operator| ( Enum other ) const

This is an overloaded function.

QFlags & QFlags::operator|= ( QFlags other )

Performs a bitwise OR operation with other and stores the result in this QFlags object. Returns a reference to this object.

See also operator|(), operator&=(), and operator^=().

QFlags & QFlags::operator|= ( Enum other )

This is an overloaded function.

QFlags QFlags::operator~ () const

Returns a QFlags object that contains the bitwise negation of this object.

See also operator&(), operator|(), and operator^().

Macro Documentation

Q_DECLARE_FLAGS ( Flags, Enum )

The Q_DECLARE_FLAGS() macro expands to

 typedef QFlags<Enum> Flags;

Enum is the name of an existing enum type, whereas Flags is the name of the QFlags<Enum> typedef.

See the QFlags documentation for details.

See also Q_DECLARE_OPERATORS_FOR_FLAGS().

Q_DECLARE_OPERATORS_FOR_FLAGS ( Flags )

The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global operator|() functions for Flags, which is of type QFlags<T>.

See the QFlags documentation for details.

See also Q_DECLARE_FLAGS().

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. Microsoft ouvre aux autres compilateurs C++ AMP, la spécification pour la conception d'applications parallèles C++ utilisant le GPU 22
  2. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  3. RIM : « 13 % des développeurs ont gagné plus de 100 000 $ sur l'AppWord », Qt et open-source au menu du BlackBerry DevCon Europe 0
  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. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
Page suivante

Le Qt Labs au hasard

Logo

La folie est de mettre en forme le même texte

Les Qt Labs sont les laboratoires des développeurs de Qt, où ils peuvent partager des impressions sur le framework, son utilisation, ce que pourrait être son futur. 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
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