enum enType{ flag1, flag2}; namespace Qt{ enType str; //тут неправильно}
namespace Qt{ enum enType { flag1, flag2 }; } int val = Qt::flag1;
C++ (Qt)namespace qt { enum { one, two };};...int v = qt::one;
C++ (Qt)void func(qt v) { // так нельзя. потому что qt, не является типом. это нэймспейс.}
C++ (Qt)namespace { const std::string str = "blablabla";}; // in the body of any functionstd::string str1 = "oops..." + str;
... enum OpenModeFlag { NotOpen = 0x0000, ReadOnly = 0x0001, WriteOnly = 0x0002, ReadWrite = ReadOnly | WriteOnly, Append = 0x0004, Truncate = 0x0008, Text = 0x0010, Unbuffered = 0x0020 }; Q_DECLARE_FLAGS(OpenMode, OpenModeFlag)...
... enum MyModeFlag { MyFlag1, MyFlag2, .... MyFlagN };...
... Q_DECLARE_FLAGS(MyMode, MyModeFlag)...
#include <QObject>class MyClass : public QObject{ Q_OBJECTpublic: MyClass(QObject *parent = 0); enum MyModeFlag { Mode1 = 0, Mode2 = 1, Mode3 = 2 }; Q_DECLARE_FLAGS(MyMode, MyModeFlag) void setMode(MyClass::MyMode m); MyClass::MyMode getMode() const;private: MyClass::MyMode m_mode;};
MyClass::MyClass(QObject *parent) : QObject(parent), m_mode(MyClass::Mode1){}void MyClass::setMode(MyClass::MyMode m){ m_mode = m;}MyClass::MyMode MyClass::getMode() const{ return m_mode;}
int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); MyClass mc; qDebug() << "Default mode: " << mc.getMode(); mc.setMode(MyClass::Mode3); qDebug() << "New mode: " << mc.getMode(); return a.exec();}
C++ (Qt)#include <QtCore/QtGlobal>#include <QtCore/QtDebug>#include <QtCore/QMetaObject>#include <QtCore/QMetaEnum> class Programmer : public QObject{ Q_OBJECTpublic: enum Language {ASM, C, CPP, PASCAL, FORTRAN, BASIC, JAVA}; Q_ENUMS(Language)}; int main(int argc, char *argv[]){ const QMetaObject &mo = Programmer::staticMetaObject; int index = mo.indexOfEnumerator("Language"); QMetaEnum me = mo.enumerator(index); Programmer::Language p = Programmer::CPP; Programmer::Language l= static_cast<Programmer::Language>(me.keyToValue("CPP")); qDebug() << me.valueToKey(p) << ":" << l; return 0;} #include "main.moc"