Q_FLAGS in namespace causes QtCreator to mark line as error

  • Hi there,
    just stumbled on something strange in my code. I have a namespace, in the namespace I define an enum for a QFlags type, so there's something like this:
    namespace Test
    enum MyEnum {meOne=0x01, meTwo=0x02};
    Q_FLAGS(MyEnum MyEnums)
    Q_DECLARE_FLAGS(MyEnums, MyEnum)

    And this compiles just fine. However, in QtCreator, the line with Q_FLAGS is underlined red and the tool tip says "expected a declaration". What's up with that? Why does it compile fine but QtCreator thinks it won't? After all, the Q_FLAGS macro expands to nothing and is only used by moc.
    Q_FLAGS inside classes work fine, it seems the namespace is a necessary condition for this phenomenon. Maybe because moc can't access namespace ("classless") flags/enums and QtCreator knows about that? Why not say it in the tool tip then?

  • Q_FLAGS only makes sense within a QObject subclass.
    See the docs for Q_FLAGS and the example there.

  • Place #include <QObject> in the top of the file. That might do the trick.
    Remember that a compiler will use totally different settings then what QtCreator might show. QtCreator is just a handy tool that shows what "might" be wrong or what is wrong from the output of the compiler. Also a new qmake run might do the trick.

  • including<QObject> didn't work, neither the qmake run.

    I suppose it really is that Q_FLAGS only makes sense in QObject-derived classes. I did know that, however, I didn't expect QtCreator to mark something as wrong if the compiler doesn't mind, and secondly then output an inappropriate tooltip.

    Thanks for your suggestions!

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.