QFlags (Class) - Qt 5.15 (2024)

template <typename Enum> class QFlags

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

Header: #include <QFlags>
qmake: QT += core

Public Types

typedef Int
typedef enum_type

Public Functions

QFlags(std::initializer_list<Enum> flags)
QFlags(QFlag flag)
QFlags(Enum flags)
QFlags()
QFlags(const QFlags<T> &other)
QFlags<T> &operator=(const QFlags<T> &other)
QFlags<T> &setFlag(Enum flag, bool on = true)
bool testFlag(Enum flag) const
QFlags::Int operator QFlags::Int() const
bool operator!() const
QFlags<T> (int mask) const
QFlags<T> (uint mask) const
QFlags<T> (Enum other) const
QFlags<T> &(int mask)
QFlags<T> &(uint mask)
QFlags<T> &(Enum mask)
QFlags<T> operator^(QFlags<T> other) const
QFlags<T> operator^(Enum other) const
QFlags<T> &operator^=(QFlags<T> other)
QFlags<T> &operator^=(Enum other)
QFlags<T> operator|(QFlags<T> other) const
QFlags<T> operator|(Enum other) const
QFlags<T> &operator|=(QFlags<T> other)
QFlags<T> &operator|=(Enum other)
QFlags<T> operator~() const

Macros

Q_DECLARE_FLAGS(Flags, Enum)
Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)

Detailed Description

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 { }, is legal:

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

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_FLAG() macro must be used:

Q_FLAG(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::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.

typedef QFlags::enum_type

Typedef for the Enum template type.

Member Function Documentation

QFlags::QFlags(std::initializer_list<Enum> flags)

Constructs a QFlags object initialized with all flags combined using the bitwise OR operator.

This function was introduced in Qt 5.4.

See also operator|=() and operator|().

QFlags::QFlags(QFlag flag)

Constructs a QFlags object initialized with the integer flag.

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.

QFlags::QFlags(Enum flags)

Constructs a QFlags object storing the flags.

QFlags::QFlags()

Constructs a QFlags object with no flags set.

This function was introduced in Qt 5.15.

QFlags::QFlags(const QFlags<T> &other)

Constructs a copy of other.

QFlags<T> &QFlags::operator=(const QFlags<T> &other)

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

QFlags<T> &QFlags::setFlag(Enum flag, bool on = true)

Sets the flag flag if on is true or unsets it if on is false. Returns a reference to this object.

This function was introduced in Qt 5.7.

bool QFlags::testFlag(Enum flag) const

Returns true if the flag flag is set, otherwise false.

This function was introduced in Qt 4.2.

QFlags::Int QFlags::operator QFlags::Int() const

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

See also Int.

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<T> 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^(), and operator~().

QFlags<T> QFlags::operator&(uint mask) const

This is an overloaded function.

QFlags<T> QFlags::operator&(Enum other) const

This is an overloaded function.

QFlags<T> &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|=(), and operator^=().

QFlags<T> &QFlags::operator&=(uint mask)

This is an overloaded function.

QFlags<T> &QFlags::operator&=(Enum mask)

This is an overloaded function.

QFlags<T> QFlags::operator^(QFlags<T> other) const

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

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

QFlags<T> QFlags::operator^(Enum other) const

This is an overloaded function.

QFlags<T> &QFlags::operator^=(QFlags<T> 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^(), (), and operator|=().

QFlags<T> &QFlags::operator^=(Enum other)

This is an overloaded function.

QFlags<T> QFlags::operator|(QFlags<T> other) const

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

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

QFlags<T> QFlags::operator|(Enum other) const

This is an overloaded function.

QFlags<T> &QFlags::operator|=(QFlags<T> 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|(), (), and operator^=().

QFlags<T> &QFlags::operator|=(Enum other)

This is an overloaded function.

QFlags<T> QFlags::operator~() const

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

See also (), 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().

© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-5.15/qflags.html

QFlags (Class) - Qt 5.15 (2024)
Top Articles
G 037 White Oblong Pill
Elanco Rebates.com 2022
Kreme Delite Menu
Craigslist Cars Augusta Ga
Limp Home Mode Maximum Derate
Aiken County government, school officials promote penny tax in North Augusta
Pickswise the Free Sports Handicapping Service 2023
Ashlyn Peaks Bio
Merlot Aero Crew Portal
Doby's Funeral Home Obituaries
What is a basic financial statement?
Jasmine Put A Ring On It Age
Ts Lillydoll
charleston cars & trucks - by owner - craigslist
7 Fly Traps For Effective Pest Control
Munich residents spend the most online for food
Does Breckie Hill Have An Only Fans – Repeat Replay
Prosser Dam Fish Count
Osborn-Checkliste: Ideen finden mit System
Www Craigslist Milwaukee Wi
Whitefish Bay Calendar
Ein Blutbad wie kein anderes: Evil Dead Rise ist der Horrorfilm des Jahres
College Basketball Picks: NCAAB Picks Against The Spread | Pickswise
Optum Urgent Care - Nutley Photos
Rochester Ny Missed Connections
Conscious Cloud Dispensary Photos
Cookie Clicker Advanced Method Unblocked
Meridian Owners Forum
1145 Barnett Drive
Panolian Batesville Ms Obituaries 2022
Rainfall Map Oklahoma
Jail Roster Independence Ks
Angela Muto Ronnie's Mom
Memberweb Bw
Lowell Car Accident Lawyer Kiley Law Group
#scandalous stars | astrognossienne
Craigslist Red Wing Mn
10 Most Ridiculously Expensive Haircuts Of All Time in 2024 - Financesonline.com
4083519708
The Transformation Of Vanessa Ray From Childhood To Blue Bloods - Looper
Busch Gardens Wait Times
Urban Blight Crossword Clue
FREE - Divitarot.com - Tarot Denis Lapierre - Free divinatory tarot - Your divinatory tarot - Your future according to the cards! - Official website of Denis Lapierre - LIVE TAROT - Online Free Tarot cards reading - TAROT - Your free online latin tarot re
Gregory (Five Nights at Freddy's)
Winta Zesu Net Worth
Holzer Athena Portal
Funkin' on the Heights
Amy Zais Obituary
How to Connect Jabra Earbuds to an iPhone | Decortweaks
Noga Funeral Home Obituaries
Jackerman Mothers Warmth Part 3
Pulpo Yonke Houston Tx
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 5957

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.