Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]How to forward declare a Q_Flag for signal/slot connections?

[solved]How to forward declare a Q_Flag for signal/slot connections?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • shaveraS Offline
    shaveraS Offline
    shavera
    wrote on last edited by shavera
    #1
    // Foo.h
    class Foo : public QObject{
        Q_OBJECT
        Q_PROPERTY(fooFlags flags READ flags WRITE setFlags NOTIFY flagsChanged)
    
    public:
        enum Flag{
            fast = 0x01,
            far = 0x02,
            furious = 0x07
        };
        Q_DECLARE_FLAGS(Flags, Flag)
        Q_FLAG(Flag)
        Q_FLAG(Flags)
    
        Foo::Flags flags() const {return m_flags;}
    
    signals:
         void flagsChanged(Flags)
    
    public slots:
        void setFlags(const Flags _flags);
    
    private:
        Flags m_flags;
    };
    Q_DECLARE_OPERATORS_FOR_FLAGS(Foo::Flags)
    
    // bar.h
    class Foo;
    
    class Bar : public QObject{
        Q_OBJECT
    public:
        Bar(Foo* _foo);
    
    private slots:
        void respondToFooFlagChange(const Foo::Flags& flags);
    
    private:
        Foo* m_foo;
    };
    
    //bar.cpp
    #include "bar.h"
    #include "foo.h"
    
    Bar(Foo* _foo)  :  m_foo{ _foo } {
        connect(*_foo, &Foo::flagsChanged,
                this, &Bar::respondToFooFlagChange);
    }
    
    void Bar::respondToFooFlagChange(const Foo::Flags& flags){
    // Do stuff
    };
    

    Suppose, for whatever design reasons, I prefer to keep Foo as a forward declared class for Bar. It won't compile because it doesn't know what "Foo::Flags" is. But at the same time, I don't know how forward declare it.

    If enum was a straight c++11 enum class, forward declaration is pretty straightforward.

    I could also hack it by downgrading to int and reconverting from int. But that seems even worse architecturally than just including the Foo header in the Bar header.

    The examples I can see in the Qt Source code seem to include headers rather than using forward declarations, so they're not much help to me.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You can't, the compiler needs to know all the values of the enum in order setup the correct data storage for it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      shaveraS 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        You can't, the compiler needs to know all the values of the enum in order setup the correct data storage for it.

        shaveraS Offline
        shaveraS Offline
        shavera
        wrote on last edited by
        #3

        @SGaist thanks, that answers that.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved