IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QFlags Class

The https:// class provides a type-safe way of storing OR-combinations of enum values.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QFlags Class▲

  • Header: QFlags

  • qmake: QT += core

  • Group: QFlags is part of tools

Detailed Description▲

The https://<Enum> class is a template class, where Enum is an enum type. https:// 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 https:// to provide type safety. For example, the Qt::Alignment type is simply a typedef for https://<Qt::AlignmentFlag>. QLabel::setAlignment() takes a Qt::Alignment parameter, which means that any combination of Qt::AlignmentFlag values, or 0, is legal:

 
Sélectionnez
label-&gt;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 https:// constructor as cast operator.

If you want to use https:// for your own enum types, use the https://() and https://().

Example:

 
Sélectionnez
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 https://() 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 https://() macro must be used:

 
Sélectionnez
Q_FLAG(Options)

Naming Convention▲

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

See Also▲

See also https://

Member Type Documentation▲

 

[since 5.0] QFlags::Int▲

Typedef for the integer type used for storage as well as for implicit conversion. Either int or unsigned int, depending on whether the enum's underlying type is signed or unsigned.

This typedef was introduced in Qt 5.0.

QFlags::enum_type▲

Typedef for the Enum template type.

Member Function Documentation▲

 

[default] QFlags::QFlags(const QFlags<Enum> &other)▲

Constructs a copy of other.

[default] QFlags::QFlags(Enum flags)▲

Constructs a https:// object storing the flags.

[default] QFlags::QFlags(QFlags::Zero)▲

Constructs a https:// object with no flags set. The parameter must be a literal 0 value.

[default] QFlags::QFlags(QFlag flag)▲

Constructs a https:// object initialized with the integer flag.

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

[default, since 5.4] QFlags::QFlags(std::initializer_list<Enum> flags)▲

Constructs a https:// object initialized with all flags combined using the bitwise OR operator.

This function was introduced in Qt 5.4.

See Also▲

See also operator|=(), operator|()

[default] int &QFlags::operator=(const QFlags<Enum> &)▲

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

Macro Documentation▲

 

Q_DECLARE_FLAGS(Flags, Enum)▲

The Q_DECLARE_FLAGS() macro expands to

 
Sélectionnez
typedef QFlags&lt;Enum&gt; Flags;

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

See the https:// documentation for details.

See Also▲

See also https://()

Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)▲

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

See the https:// documentation for details.

See Also▲

See also https://()

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.