Using Qt signals and slots with multiple inheritance

前端 未结 2 613
野的像风
野的像风 2021-01-02 09:52

I have a class (MyClass) that inherits most of its functionality from a Qt built-in object (QGraphicsTextItem). QGraphicsTextItem inhe

2条回答
  •  暖寄归人
    2021-01-02 10:25

    You can declare MyInterface that takes a QObject in its constructor:

    class MyInterface {
    public:
                    MyInterface(QObject * object);
        QObject *   object() { return m_object; }
        ...
    private:
        QObject *   m_object;
    };
    
    MyInterface::MyInterface(QObject * object) :
        m_object(object)
    {
        ...
    }
    

    Then in MyClass constructor:

    MyClass::MyClass() :
    MyInterface(this)
    {
        ...
    }
    

    And you can connect the signal:

    MyInterface *my_interface_instance = GetInstance();
    connect(my_interface_instance->object(), SIGNAL(MyInterfaceSignal()), this, SLOT(TempSlot()));
    

提交回复
热议问题