Here is something that works for me:

class SomeInterface
{
  public:
    virtual void someMethod() const;
};

class Some : public QObject, public SomeInterface
{
  Q_OBJECT
  public:
  signals:
    void someMethod() const override;
}

Pointer to the interface is used by a pure C++ library. The concrete object
is created in Qt and passed to the lib via a function. When the library
calls someMethod(), the signal is emitted - it works :-)

On Mon, 18 Feb 2019 at 15:28, Jérôme Godbout <godbo...@amotus.ca> wrote:

> I understand, but if you need your class to inherit something higher then
> QObject directly (QFile, QDataStream, …) you get screw. I also get screw if
> a class inherite multiple interface. I did pure virtual function that
> simply emit the signals, but it’s ugly, I did not find any better way so
> far.
>
>
>
>
> [image: 36E56279]
>
> une compagnie
>
> RAPPROCHEZ LA DISTANCE
>
> *Jérôme Godbout*
> Développeur Logiciel Sénior /
> Senior Software Developer
>
> *p:* +1 (418) 800-1073 ext.:109
>
> amotus.ca <http://www.amotus-solutions.com/>
> statum-iot.com
>
> <https://www.facebook.com/LesSolutionsAmotus/>
> <https://www.linkedin.com/company/amotus-solutions/>
> <https://twitter.com/AmotusSolutions>
> <https://www.youtube.com/channel/UCoYpQgsmj1iJZyDjTQ3x8Ig>
>
>
>
>
>
> *From:* Kristoffersen, Even (NO14) <even.kristoffer...@honeywell.com>
> *Sent:* February 18, 2019 8:50 AM
> *To:* interest@qt-project.org
> *Cc:* Jérôme Godbout <godbo...@amotus.ca>
> *Subject:* RE: [External] [Interest] Interface with signals and slots
>
>
>
> Really late response, but why inherit QObject in MyClass rather than
> IMyInterface?
>
>
>
> Since all the objects inheriting from IMyInterface needs to be QObjects
> you can just do this:
>
>
>
> *class IMyInterface : public QObject { Q_OBJECT etc… }*
>
> *class MyClass : public IMyInterface {  Q_OBJECT and so on… }*
>
>
>
>
>
> -Even
>
>
>
> *From:* Interest [mailto:interest-boun...@lists.qt-project.org
> <interest-boun...@lists.qt-project.org>] *On Behalf Of *Jérôme Godbout
> *Sent:* 30. november 2018 21:38
> *To:* interest@qt-project.org
> *Subject:* [External] [Interest] Interface with signals and slots
>
>
>
> Hi,
>
> I was wondering if it’s even possible to do the following somehow. I'm
> trying to make an Interface with signals without inheriting the QObject and
> I have a hard time make this compile properly:
>
>
>
> *class IMyInterface*
>
> *{*
>
> *public:*
>
> *    explicit IMyInterface();*
>
> *    virtual ~IMyInterface();*
>
>
>
> *    bool val() const;*
>
> *    void setVal(const bool v);*
>
>
>
> *signals:*
>
> *    virtual void valChanged() = 0;*
>
>
>
> *protected:*
>
> *    bool m_val;*
>
> *};*
>
>
>
> *Q_DECLARE_INTERFACE(IMyInterface, "IMyInterface")*
>
>
>
> *class MyClass : public QObject, public IMyInterface*
>
> *{*
>
> *    Q_OBJECT*
>
> *    Q_PROPERTY(bool val READ val WRITE setVal NOTIFY valChanged)*
>
> *    Q_INTERFACES(IMyInterface)*
>
>
>
> *public:*
>
> *    ...*
>
>
>
> *signals:*
>
> *    virtual void valChanged() override;*
>
> *};*
>
>
>
> *Q_DECLARE_METATYPE(MyClass*)*
>
>
>
> The example is simplified to show my problem, the different class will
> inherit different QObject type so I cannot inherit 2 different QObject
> type. It does compile but this fail at link with the moc:
>
>
>
> *moc_MyClass.obj:moc_MyClass.cpp:vtable for MyClass: error: undefined
> reference to 'MyClass::dirtyChanged()'*
>
> *moc_MyClass.obj:moc_MyClass.cpp:vtable for MyClass: error: undefined
> reference to 'non-virtual thunk to MyClass::valChanged()'*
>
> *module_demo.obj:module_demo.cpp:vtable for
> QQmlPrivate::QQmlElement<MyClass>: error: undefined reference to
> 'MyClass::valChanged()'*
>
> *module_demo.obj:module_demo.cpp:vtable for
> QQmlPrivate::QQmlElement<MyClass>: error: undefined reference to
> 'non-virtual thunk to MyClass::valChanged()'*
>
>
>
> Is there a way to use the signals/slots into an interface to be declared?
> That would greatly help me do reusable code part.
>
>
>
>
> [image: 36E56279]
>
> une compagnie
>
> RAPPROCHEZ LA DISTANCE
>
> *Jérôme Godbout*
> Développeur Logiciel Sénior /
> Senior Software Developer
>
> *p:* +1 (418) 800-1073 ext.:109
>
> amotus.ca <http://www.amotus-solutions.com/>
> statum-iot.com
>
> <https://www.facebook.com/LesSolutionsAmotus/>
> <https://www.linkedin.com/company/amotus-solutions/>
> <https://twitter.com/AmotusSolutions>
> <https://www.youtube.com/channel/UCoYpQgsmj1iJZyDjTQ3x8Ig>
>
>
>
>
> _______________________________________________
> Interest mailing list
> Interest@qt-project.org
> https://lists.qt-project.org/listinfo/interest
>
_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to