[Interest] [External] Interface with signals and slots (Jérôme Godbout)

Jérôme Godbout godboutj at amotus.ca
Tue Feb 19 05:51:03 CET 2019

Yeah I get that, but I loose all the generic implementation from the interface I could have done and I have to rewrite them. I did make some abstract methods and header only template declaration that the abstract class can call, it's just that I was looking at a way to have some concrete method along the interface. That would save me many lines of useless code, the example with get set are simple but I'm having methods that have to do a lot more.

But I guess it's the best we can do with abstract signals, is only to make them exist but never call them until the class is abstract and a QObject. So at that point the abstract signal is pretty much pointless since it serve no purpose, you better check meta object and check for a signal name and signature to call/connect it.

So it leave us with a few choices I see so far:

  *   inline template header function that will you do the function in all abstract call
  *   create Macro that will generate the code into all the abstract class

I usually mix both to make this work and avoid  code cluttering.

I wish I could create those easily and assemble my QObject by features compositions (interfaces here that would have properties, methods, signals, slots). Since we can only inherit a single QObject, making a composition is not so easy, I feel I have to rewrite the same features over and over again to different Object type just because they do not inherit the same base QObject class but they just need the same features.

Thanks anyway,

From: Interest <interest-bounces at qt-project.org> on behalf of Stan Morris <pixelgrease at gmail.com>
Sent: Monday, February 18, 2019 9:43 PM
To: interest at qt-project.org
Subject: Re: [Interest] [External] Interface with signals and slots (Jérôme Godbout)

Re: [External]  Interface with signals and slots
      (Jérôme Godbout)

Your sample code declares an abstract IMyInterface rather than a class interface; there are concrete method declarations. If you make them pure virtual methods, your code compiles. You must implement the interface functions in the subclass... that's just the way it is. You can inherit multiple interfaces by implementing the getter/setters in the subclass.

Here are code changes that work:

class IMyInterface



    explicit IMyInterface() {};

    virtual ~IMyInterface() {};

    virtual bool val() const = 0;

    virtual void setVal(const bool v) = 0;


    virtual void valChanged() const = 0;


class MyClass : public QObject, public IMyInterface



    Q_PROPERTY(bool val READ val WRITE setVal NOTIFY valChanged)



    MyClass() : m_val() {}

    bool val() const override { return m_val; }

    void setVal(const bool v) override


        if (v != m_val) {

            m_val = v;

            emit valChanged();




    virtual void valChanged() const override;


    bool m_val;


- Stan

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190219/f08d8022/attachment.html>

More information about the Interest mailing list