QFlags tutorial: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Simple tutorial for safe-usage QFlags=
[[Category:snippets]]


==Overview==
[toc align_right="yes" depth="3"]


First of all we should write about macro <code>Q_FLAGS</code><br />''This macro registers one or several flags types to the meta-object system''
= Simple tutorial for safe-usage QFlags =


==Example:==
== Overview ==


The <code>Q_DECLARE_OPERATORS_FOR_FLAGS()</code> macro declares global <code>operator|()</code> functions for Flags, which is of type <code>QFlags&lt;T&gt;</code>.
First of all we should write about macro &lt;code&amp;gt;Q_FLAGS&amp;lt;/code&amp;gt;<br />''This macro registers one or several flags types to the meta-object system''


The <code>Q_DECLARE_FLAGS()</code> 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 these purpose, the <code>Q_FLAGS()</code> macro must be used.
== Example: ==


==Usage sample==
<code>class TestClass<br /> {<br /> public:<br /> enum Option {<br /> OptionA = 1, // 000001<br /> OptionB = 2, // 000010<br /> OptionC = 4, // 000100<br /> OptionD = 8, // 001000<br /> OptionE = 16, // 010000<br /> OptionF = 32 // 100000<br /> // … some more options with value which is a power of two<br /> };<br /> Q_DECLARE_FLAGS(Options, Option)<br /> };


<code>testFlag(flag)</code> method checks if flag is set in <code>QFlags</code>.
Q_DECLARE_OPERATORS_FOR_FLAGS(TestClass::Options)</code>


===Some example===
The &lt;code&amp;gt;Q_DECLARE_FLAGS()&lt;/code&amp;gt; macro expands to <code>typedef QFlags&amp;lt;Enum&amp;gt; Flags;<code> In our case it expandes to &lt;code&amp;gt;typedef QFlags&amp;lt;Option&amp;gt; Options;&lt;/code&amp;gt; where '''Option''' - is an enum name, and '''Options''' - is name for set of flags.


===Categories:===
The &lt;code&amp;gt;Q_DECLARE_OPERATORS_FOR_FLAGS()&lt;/code&amp;gt; macro declares global &lt;code&amp;gt;operator|()&lt;/code&amp;gt; functions for Flags, which is of type &lt;code&amp;gt;QFlags&amp;lt;T&amp;gt;&lt;/code&amp;gt;.


* [[:Category:snippets|snippets]]
The &lt;code&amp;gt;Q_DECLARE_FLAGS()&lt;/code&amp;gt; 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 these purpose, the &lt;code&amp;gt;Q_FLAGS()&lt;/code&amp;gt; macro must be used.
 
== Usage sample ==
 
</code>void test (TestClass::Options flag)<br />{<br /> if (flag.testFlag(TestClass::OptionA))<br /> qDebug() &lt;&lt; &quot;A&amp;quot;;<br /> if (flag.testFlag(TestClass::OptionB))<br /> qDebug() &lt;&lt; &quot;B&amp;quot;;<br />}
 
int main()<br />{<br /> test (TestClass::OptionA | TestClass::OptionB);<br /> test (0x1); // error<br />}<code>
 
&lt;code&amp;gt;testFlag(flag)&lt;/code&amp;gt; method checks if flag is set in &lt;code&amp;gt;QFlags&amp;lt;/code&amp;gt;.
 
=== Some example ===
 
</code>TestClass::Options f1(TestClass::OptionA | TestClass::OptionB); // 000011<br />TestClass::Options f2(~f1); // 111100<br />TestClass::Options f3(Foo::OptionA | Foo::OptionC); // 000101<br />TestClass::Options f4(f1^f3); // 000110

Revision as of 14:37, 23 February 2015


[toc align_right="yes&quot; depth="3&quot;]

Simple tutorial for safe-usage QFlags

Overview

First of all we should write about macro <code&gt;Q_FLAGS&lt;/code&gt;
This macro registers one or several flags types to the meta-object system

Example:

class TestClass<br /> {<br /> public:<br /> enum Option {<br /> OptionA = 1, // 000001<br /> OptionB = 2, // 000010<br /> OptionC = 4, // 000100<br /> OptionD = 8, // 001000<br /> OptionE = 16, // 010000<br /> OptionF = 32 // 100000<br /> // … some more options with value which is a power of two<br /> };<br /> Q_DECLARE_FLAGS(Options, Option)<br /> };

Q_DECLARE_OPERATORS_FOR_FLAGS(TestClass::Options)

The <code&gt;Q_DECLARE_FLAGS()</code&gt; macro expands to

typedef QFlags&amp;lt;Enum&amp;gt; Flags;<code> In our case it expandes to &lt;code&amp;gt;typedef QFlags&amp;lt;Option&amp;gt; Options;&lt;/code&amp;gt; where '''Option''' - is an enum name, and '''Options''' - is name for set of flags.

The &lt;code&amp;gt;Q_DECLARE_OPERATORS_FOR_FLAGS()&lt;/code&amp;gt; macro declares global &lt;code&amp;gt;operator|()&lt;/code&amp;gt; functions for Flags, which is of type &lt;code&amp;gt;QFlags&amp;lt;T&amp;gt;&lt;/code&amp;gt;.

The &lt;code&amp;gt;Q_DECLARE_FLAGS()&lt;/code&amp;gt; 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 these purpose, the &lt;code&amp;gt;Q_FLAGS()&lt;/code&amp;gt; macro must be used.

== Usage sample ==

void test (TestClass::Options flag)
{
if (flag.testFlag(TestClass::OptionA))
qDebug() << "A&quot;;
if (flag.testFlag(TestClass::OptionB))
qDebug() << "B&quot;;
} int main()
{
test (TestClass::OptionA | TestClass::OptionB);
test (0x1); // error
}

&lt;code&amp;gt;testFlag(flag)&lt;/code&amp;gt; method checks if flag is set in &lt;code&amp;gt;QFlags&amp;lt;/code&amp;gt;.

=== Some example ===

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