Qt 4.8
Public Types | Public Functions | Private Types | Properties | List of all members
QFlags< Enum > Class Template Reference

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

#include <qglobal.h>

Public Types

typedef Enum enum_type
 Typedef for the Enum template type. More...
 

Public Functions

Q_DECL_CONSTEXPR QFlags operator & (int mask) const
 
Q_DECL_CONSTEXPR QFlags operator & (uint mask) const
 
Q_DECL_CONSTEXPR QFlags operator & (Enum f) const
 
QFlagsoperator &= (int mask)
 
QFlagsoperator &= (uint mask)
 
Q_DECL_CONSTEXPR operator int () const
 Returns the value stored in the QFlags object as an integer. More...
 
Q_DECL_CONSTEXPR bool operator! () const
 Returns true if no flag is set (i. More...
 
QFlagsoperator= (const QFlags &f)
 Assigns other to this object and returns a reference to this object. More...
 
Q_DECL_CONSTEXPR QFlags operator^ (QFlags f) const
 Returns a QFlags object containing the result of the bitwise XOR operation on this object and other. More...
 
Q_DECL_CONSTEXPR QFlags operator^ (Enum f) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
QFlagsoperator^= (QFlags f)
 Performs a bitwise XOR operation with other and stores the result in this QFlags object. More...
 
QFlagsoperator^= (Enum f)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
Q_DECL_CONSTEXPR QFlags operator| (QFlags f) const
 Returns a QFlags object containing the result of the bitwise OR operation on this object and other. More...
 
Q_DECL_CONSTEXPR QFlags operator| (Enum f) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
QFlagsoperator|= (QFlags f)
 Performs a bitwise OR operation with other and stores the result in this QFlags object. More...
 
QFlagsoperator|= (Enum f)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
Q_DECL_CONSTEXPR QFlags operator~ () const
 Returns a QFlags object that contains the bitwise negation of this object. More...
 
Q_DECL_CONSTEXPR QFlags (const QFlags &f)
 Constructs a copy of other. More...
 
Q_DECL_CONSTEXPR QFlags (Enum f)
 Constructs a QFlags object storing the given flag. More...
 
Q_DECL_CONSTEXPR QFlags (Zero=0)
 Constructs a QFlags object with no flags set. More...
 
 QFlags (QFlag f)
 Constructs a QFlags object initialized with the given integer value. More...
 
bool testFlag (Enum f) const
 Returns true if the flag is set, otherwise false. More...
 

Private Types

typedef void ** Zero
 

Properties

int i
 

Detailed Description

template<typename Enum>
class QFlags< Enum >

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)
...
};

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

Definition at line 2313 of file qglobal.h.

Typedefs

◆ enum_type

template<typename Enum>
QFlags< Enum >::enum_type

Typedef for the Enum template type.

Definition at line 2318 of file qglobal.h.

◆ Zero

template<typename Enum>
typedef void** QFlags< Enum >::Zero
private

Definition at line 2315 of file qglobal.h.

Constructors and Destructors

◆ QFlags() [1/4]

template<typename Enum>
QFlags< Enum >::QFlags ( const QFlags< Enum > &  f)
inline

Constructs a copy of other.

Definition at line 2319 of file qglobal.h.

2319 : i(f.i) {}
int i
Definition: qglobal.h:2316

◆ QFlags() [2/4]

template<typename Enum>
QFlags< Enum >::QFlags ( Enum  f)
inline

Constructs a QFlags object storing the given flag.

Definition at line 2320 of file qglobal.h.

2320 : i(f) {}
int i
Definition: qglobal.h:2316

◆ QFlags() [3/4]

template<typename Enum>
QFlags< Enum >::QFlags ( Zero  zero = 0)
inline

Constructs a QFlags object with no flags set.

zero must be a literal 0 value.

Definition at line 2321 of file qglobal.h.

2321 : i(0) {}
int i
Definition: qglobal.h:2316

◆ QFlags() [4/4]

template<typename Enum>
QFlags< Enum >::QFlags ( QFlag  value)
inline

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.

Definition at line 2322 of file qglobal.h.

2322 : i(f) {}
int i
Definition: qglobal.h:2316

Functions

◆ operator &() [1/3]

template<typename Enum>
Q_DECL_CONSTEXPR QFlags QFlags< Enum >::operator& ( int  mask) const
inline

Definition at line 2338 of file qglobal.h.

2338 { return QFlags(Enum(i & mask)); }
int i
Definition: qglobal.h:2316
Q_DECL_CONSTEXPR QFlags(const QFlags &f)
Constructs a copy of other.
Definition: qglobal.h:2319

◆ operator &() [2/3]

template<typename Enum>
Q_DECL_CONSTEXPR QFlags QFlags< Enum >::operator& ( uint  mask) const
inline

Definition at line 2339 of file qglobal.h.

2339 { return QFlags(Enum(i & mask)); }
int i
Definition: qglobal.h:2316
Q_DECL_CONSTEXPR QFlags(const QFlags &f)
Constructs a copy of other.
Definition: qglobal.h:2319

◆ operator &() [3/3]

template<typename Enum>
Q_DECL_CONSTEXPR QFlags QFlags< Enum >::operator& ( Enum  f) const
inline

Definition at line 2340 of file qglobal.h.

2340 { return QFlags(Enum(i & f)); }
int i
Definition: qglobal.h:2316
Q_DECL_CONSTEXPR QFlags(const QFlags &f)
Constructs a copy of other.
Definition: qglobal.h:2319

◆ operator &=() [1/2]

template<typename Enum>
QFlags& QFlags< Enum >::operator&= ( int  mask)
inline

Definition at line 2325 of file qglobal.h.

2325 { i &= mask; return *this; }
int i
Definition: qglobal.h:2316

◆ operator &=() [2/2]

template<typename Enum>
QFlags& QFlags< Enum >::operator&= ( uint  mask)
inline

Definition at line 2326 of file qglobal.h.

2326 { i &= mask; return *this; }
int i
Definition: qglobal.h:2316

◆ operator int()

template<typename Enum>
QFlags< Enum >::operator int ( ) const
inline

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

Definition at line 2332 of file qglobal.h.

2332 { return i; }
int i
Definition: qglobal.h:2316

◆ operator!()

template<typename Enum>
bool QFlags< Enum >::operator! ( ) const
inline

Returns true if no flag is set (i.

e., if the value stored by the QFlags object is 0); otherwise returns false.

Definition at line 2343 of file qglobal.h.

2343 { return !i; }
int i
Definition: qglobal.h:2316

◆ operator=()

template<typename Enum>
QFlags & QFlags< Enum >::operator= ( const QFlags< Enum > &  f)
inline

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

Definition at line 2324 of file qglobal.h.

2324 { i = f.i; return *this; }
int i
Definition: qglobal.h:2316

◆ operator^() [1/2]

template<typename Enum>
QFlags QFlags< Enum >::operator^ ( QFlags< Enum >  other) const
inline

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

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

Definition at line 2336 of file qglobal.h.

2336 { return QFlags(Enum(i ^ f.i)); }
int i
Definition: qglobal.h:2316
Q_DECL_CONSTEXPR QFlags(const QFlags &f)
Constructs a copy of other.
Definition: qglobal.h:2319

◆ operator^() [2/2]

template<typename Enum>
QFlags QFlags< Enum >::operator^ ( Enum  f) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2337 of file qglobal.h.

2337 { return QFlags(Enum(i ^ f)); }
int i
Definition: qglobal.h:2316
Q_DECL_CONSTEXPR QFlags(const QFlags &f)
Constructs a copy of other.
Definition: qglobal.h:2319

◆ operator^=() [1/2]

template<typename Enum>
QFlags & QFlags< Enum >::operator^= ( QFlags< Enum >  other)
inline

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&=(), operator|=()

Definition at line 2329 of file qglobal.h.

2329 { i ^= f.i; return *this; }
int i
Definition: qglobal.h:2316

◆ operator^=() [2/2]

template<typename Enum>
QFlags & QFlags< Enum >::operator^= ( Enum  f)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2330 of file qglobal.h.

2330 { i ^= f; return *this; }
int i
Definition: qglobal.h:2316

◆ operator|() [1/2]

template<typename Enum>
QFlags QFlags< Enum >::operator| ( QFlags< Enum >  other) const
inline

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

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

Definition at line 2334 of file qglobal.h.

2334 { return QFlags(Enum(i | f.i)); }
int i
Definition: qglobal.h:2316
Q_DECL_CONSTEXPR QFlags(const QFlags &f)
Constructs a copy of other.
Definition: qglobal.h:2319

◆ operator|() [2/2]

template<typename Enum>
QFlags QFlags< Enum >::operator| ( Enum  f) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2335 of file qglobal.h.

2335 { return QFlags(Enum(i | f)); }
int i
Definition: qglobal.h:2316
Q_DECL_CONSTEXPR QFlags(const QFlags &f)
Constructs a copy of other.
Definition: qglobal.h:2319

◆ operator|=() [1/2]

template<typename Enum>
QFlags & QFlags< Enum >::operator|= ( QFlags< Enum >  other)
inline

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&=(), operator^=()

Definition at line 2327 of file qglobal.h.

2327 { i |= f.i; return *this; }
int i
Definition: qglobal.h:2316

◆ operator|=() [2/2]

template<typename Enum>
QFlags & QFlags< Enum >::operator|= ( Enum  f)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2328 of file qglobal.h.

2328 { i |= f; return *this; }
int i
Definition: qglobal.h:2316

◆ operator~()

template<typename Enum>
QFlags QFlags< Enum >::operator~ ( ) const
inline

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

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

Definition at line 2341 of file qglobal.h.

2341 { return QFlags(Enum(~i)); }
int i
Definition: qglobal.h:2316
Q_DECL_CONSTEXPR QFlags(const QFlags &f)
Constructs a copy of other.
Definition: qglobal.h:2319

◆ testFlag()

template<typename Enum>
bool QFlags< Enum >::testFlag ( Enum  flag) const
inline

Properties

◆ i

template<typename Enum>
int QFlags< Enum >::i
private

The documentation for this class was generated from the following files: