QFlags使用
#define Q_DECLARE_FLAGS(Flags, Enum)\
typedef QFlags<Enum> Flags;
#ifndef TESTCLASS_H
#define TESTCLASS_H
#include <QFlags>
class TestClass
{
public:
enum Option {
OptionA = 0x1, // 0x000001
OptionB = 0x2, // 0x000010
OptionC = 0x4, // 0x000100
OptionD = 0x8, // 0x001000
OptionE = 0x10 // 0x010000
// ... some more options with value which is a power of two
};
Q_DECLARE_FLAGS(Options, Option)
};
Q_DECLARE_OPERATORS_FOR_FLAGS(TestClass::Options)
#endif // TESTCLASS_H
#include <QtCore/QCoreApplication>
#include "testclass.h"
#include <QDebug>
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
}
TODO :Q_FLAGS()
宏不知道怎么用。。。。