Qt multiple inheritance and signals

五迷三道 提交于 2019-12-03 07:17:15

It requires a bit more code, but what I have done in the past is make one of them (your NavigatableItem in this case) a pure virtual class, i.e. interface. Instead of using the "signals" macro, make them pure virtual protected functions. Then multiply-inherit from your QObject-derived class as well as the interface, and implement the methods.

I know it is somewhat controversial, but avoiding multiple implementation inheritance at all costs does solve a host of problems and confusion. The Google C++ Style Guidelines recommend this, and I think it is good advice.

class NavigatableItemInterface
{
    // Don't forget the virtual destructor!
    protected:
        virtual void deselected() = 0;
        virtual void selected() = 0;
        virtual void activated() = 0;
};

class Button : public NavigatableItemInterface, public QToolButton
{
    Q_OBJECT
    ...
    signals:
        virtual void deselected();
        ...
}
Chris Card

Use virtual inheritence, e.g.

class X : public virtual Y 
{
};

class Z : public virtual Y
{
};

class A : public virtual X, public virtual Z
{
};

will only have one copy of the base class Y

snoofkin

You should use virtual inheritance.

see http://en.allexperts.com/q/C-1040/virtual-inheritance.htm

Seems like you are experiencing the diamond issue, see also:

http://www.cprogramming.com/tutorial/virtual_inheritance.html

Do it like this:

class NavigatableItem : public virtual QObject
class Button : public virtual NavigatableItem, public virtual QToolButton
class MainMenuOption : public Button
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!