[Interest] [External] Interface with signals and slots

Jérôme Godbout godboutj at amotus.ca
Mon Feb 18 15:25:46 CET 2019


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.


[36E56279]
une compagnie  [cid:image002.jpg at 01D4C76B.ECA213E0]
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<http://statum-iot.com/>
[cid:image003.png at 01D4C76B.ECA213E0]<https://www.facebook.com/LesSolutionsAmotus/> [cid:image004.png at 01D4C76B.ECA213E0] <https://www.linkedin.com/company/amotus-solutions/>  [cid:image005.png at 01D4C76B.ECA213E0] <https://twitter.com/AmotusSolutions>  [cid:image006.jpg at 01D4C76B.ECA213E0] <https://www.youtube.com/channel/UCoYpQgsmj1iJZyDjTQ3x8Ig>





From: Kristoffersen, Even (NO14) <Even.Kristoffersen at Honeywell.com>
Sent: February 18, 2019 8:50 AM
To: interest at qt-project.org
Cc: Jérôme Godbout <godboutj at 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-bounces at lists.qt-project.org] On Behalf Of Jérôme Godbout
Sent: 30. november 2018 21:38
To: interest at qt-project.org<mailto:interest at 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.


[36E56279]
une compagnie  [cid:image014.jpg at 01D4C76B.EC8F7640]
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<http://statum-iot.com/>
[cid:image015.png at 01D4C76B.EC8F7640]<https://www.facebook.com/LesSolutionsAmotus/> [cid:image016.png at 01D4C76B.EC8F7640] <https://www.linkedin.com/company/amotus-solutions/>  [cid:image017.png at 01D4C76B.EC8F7640] <https://twitter.com/AmotusSolutions>  [cid:image018.jpg at 01D4C76B.EC8F7640] <https://www.youtube.com/channel/UCoYpQgsmj1iJZyDjTQ3x8Ig>





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image013.jpg
Type: image/jpeg
Size: 6751 bytes
Desc: image013.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image014.jpg
Type: image/jpeg
Size: 1016 bytes
Desc: image014.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment-0001.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image015.png
Type: image/png
Size: 483 bytes
Desc: image015.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image016.png
Type: image/png
Size: 506 bytes
Desc: image016.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image017.png
Type: image/png
Size: 500 bytes
Desc: image017.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image018.jpg
Type: image/jpeg
Size: 713 bytes
Desc: image018.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment-0002.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 6751 bytes
Desc: image001.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment-0003.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.jpg
Type: image/jpeg
Size: 1016 bytes
Desc: image002.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment-0004.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.png
Type: image/png
Size: 483 bytes
Desc: image003.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment-0003.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image004.png
Type: image/png
Size: 506 bytes
Desc: image004.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment-0004.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image005.png
Type: image/png
Size: 500 bytes
Desc: image005.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment-0005.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image006.jpg
Type: image/jpeg
Size: 713 bytes
Desc: image006.jpg
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20190218/1e2cb88c/attachment-0005.jpg>


More information about the Interest mailing list