2

Documentation: http://doc.qt.io/qt-5/qflags.html#operator-Int

The question. I want to know what flags are set withouth testing one by one so I want the int number. Can anyone provide an example of how to use that operator in one of the many qt methods that rerturn a QFlags?

aarelovich
  • 5,140
  • 11
  • 55
  • 106

1 Answers1

3

By referring to QFlags.h source code (https://android.googlesource.com/platform/prebuilts/android-emulator-build/qt/+/master/common/include/QtCore/qflags.h)

This is the definition in QFlags for "Int" operator.

Q_DECL_CONSTEXPR inline operator Int() const Q_DECL_NOTHROW { return i; }

And the "i" in return statement is declared as

Int i;

And the "Int" is declared as

typedef int Int

Notice the below two constructors of QFlags. The first constructor takes Enum as parameter and the second constructor takes QFlag as parameter.

Q_DECL_CONSTEXPR inline QFlags(Enum f) Q_DECL_NOTHROW : i(Int(f)) {}
Q_DECL_CONSTEXPR inline QFlags(QFlag f) Q_DECL_NOTHROW : i(f) {}

After noticing the above constructors, if Enum is passed to the constructor, the Enum can be a signed one or unsigned one. QFlags internally type casts it to int using Int.

Consider below example now.

//Qt::CursorShape is an Enum
Qt::CursorShape shape = Qt::ArrowCursor;

//Create QFlags object by passing "ENUM" as parameter
QFlags<Qt::CursorShape> qF(shape);

//Create QFlags object by just passing FLAG as a parameter
QFlags<Qt::CursorShape> q(Qt::ArrowCursor);

Now the situation where "Int" operator is called: In the below piece of code the first statement invokes Int operator and not in the second statement.

//Now try getting the values.
int test = qF; //In this case the "Int" operator is called.
int test1 = q;
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34