Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
QFlags tutorial: Difference between revisions
m (Fixed misaligned code tags and some minor wording.) |
m (Fix small typos) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:Snippets::Misc]] | |||
[[Category:Tutorial]] | |||
==Overview== | |||
== Overview == | |||
First of all we should write about the macro: | First of all we should write about the macro: | ||
Line 10: | Line 8: | ||
''This macro registers one or several flags types to the meta-object system'' | ''This macro registers one or several flags types to the meta-object system'' | ||
== Example: == | ==Example:== | ||
<code>class TestClass | <code>class TestClass | ||
Line 26: | Line 24: | ||
Q_DECLARE_FLAGS(Options, Option) | Q_DECLARE_FLAGS(Options, Option) | ||
}; | }; | ||
Q_DECLARE_OPERATORS_FOR_FLAGS(TestClass::Options)</code> | Q_DECLARE_OPERATORS_FOR_FLAGS(TestClass::Options)</code> | ||
The | The '''Q_DECLARE_FLAGS()''' macro expands to '''typedef QFlags<Enum> Flags;''' In our case it expands to '''typedef QFlags<Option> Options;''' where '''Option''' - is an enum name, and '''Options''' - is name for set of flags. | ||
The | The '''Q_DECLARE_OPERATORS_FOR_FLAGS()''' macro declares global '''operator|()''' functions for Flags, which is of type '''QFlags<T>'''. | ||
The | The '''Q_DECLARE_FLAGS()''' macro does not expose the flags to the meta-object system, so they cannot be used by Qt Script. To make the flags available for this purpose, the '''Q_FLAGS()''' macro must be used. | ||
== Usage sample == | ==Usage sample== | ||
<code>void test (TestClass::Options flag) | <code>void test (TestClass::Options flag) | ||
Line 49: | Line 48: | ||
test (TestClass::OptionA | TestClass::OptionB); | test (TestClass::OptionA | TestClass::OptionB); | ||
test (0x1); // error | test (0x1); // error | ||
}</code> | }</code> | ||
The | The '''testFlag(flag)''' method checks if flag is set in '''QFlags'''. | ||
=== Some examples === | ===Some examples=== | ||
<code>TestClass::Options f1(TestClass::OptionA | TestClass::OptionB); // 000011 | <code>TestClass::Options f1(TestClass::OptionA | TestClass::OptionB); // 000011 | ||
Line 59: | Line 59: | ||
TestClass::Options f3(Foo::OptionA | Foo::OptionC); // 000101 | TestClass::Options f3(Foo::OptionA | Foo::OptionC); // 000101 | ||
TestClass::Options f4(f1^f3); // 000110 | TestClass::Options f4(f1^f3); // 000110 | ||
f4.setFlag(TestClass::OptionF).setFlag(TestClass::OptionB, false); //100100, introduced in Qt 5.7 | |||
</code> | </code> |
Latest revision as of 20:52, 27 October 2020
Overview
First of all we should write about the macro:
Q_FLAGS
This macro registers one or several flags types to the meta-object system
Example:
class TestClass
{
public:
enum Option {
OptionA = 1, // 000001
OptionB = 2, // 000010
OptionC = 4, // 000100
OptionD = 8, // 001000
OptionE = 16, // 010000
OptionF = 32 // 100000
// … some more options with value which is a power of two
};
Q_DECLARE_FLAGS(Options, Option)
};
Q_DECLARE_OPERATORS_FOR_FLAGS(TestClass::Options)
The Q_DECLARE_FLAGS() macro expands to typedef QFlags<Enum> Flags; In our case it expands to typedef QFlags<Option> Options; where Option - is an enum name, and Options - is name for set of flags.
The Q_DECLARE_OPERATORS_FOR_FLAGS() macro declares global operator|() functions for Flags, which is of type QFlags<T>.
The Q_DECLARE_FLAGS() macro does not expose the flags to the meta-object system, so they cannot be used by Qt Script. To make the flags available for this purpose, the Q_FLAGS() macro must be used.
Usage sample
void test (TestClass::Options flag)
{
if (flag.testFlag(TestClass::OptionA))
qDebug() << "A";
if (flag.testFlag(TestClass::OptionB))
qDebug() << "B";
}
int main()
{
test (TestClass::OptionA | TestClass::OptionB);
test (0x1); // error
}
The testFlag(flag) method checks if flag is set in QFlags.
Some examples
TestClass::Options f1(TestClass::OptionA | TestClass::OptionB); // 000011
TestClass::Options f2(~f1); // 111100
TestClass::Options f3(Foo::OptionA | Foo::OptionC); // 000101
TestClass::Options f4(f1^f3); // 000110
f4.setFlag(TestClass::OptionF).setFlag(TestClass::OptionB, false); //100100, introduced in Qt 5.7