Replace Qt slot in subclass

I have a base class that defines a Qt slot

class Base
{
public:
    Base()
    {
        connect(otherobject, SIGNAL(mySignal), this, SLOT(mySlot));
    }
public slots:
    virtual void mySlot()
    {}
}

Subclass A simply implements some other things. Subclass B Overlays Slot

class SubB : Base
{
public:
    SubB() : Base() 
    {
        // Necessary?
        connect(otherobject, SIGNAL(mySignal), this, SLOT(mySlot));
    }
public slots:
    virtual void mySlot() override
    {}
}

Does slot redefinition replace a connection that was made earlier in the Bass constructor (Ie No connection in SubB needed)?

+4
source share
2 answers

You do not need to put the same connectin the constructor of the subclass. When an object is created SubB, the right slot (the one that is redefined in SubB) will be connected connectin the constructor Base.

+4
source

: . ( ++), ( Qt). Derived, , .

, ++, , , , , . , , , ++.

:

class Base : public QObject
{
  Q_OBJECT
public:
  Base(QObject * src, QObject * parent = 0) : QObject(parent)
  { connect(src, SIGNAL(mySignal), SLOT(mySlot)); }
  Q_SLOT virtual void mySlot() {}
};

class Derived : public Base
{
  Q_OBJECT
public:
  Derived(QObject * src, QObject * parent = 0) : Base(src, parent) {}
  void mySlot() Q_DECL_OVERRIDE { ... }
};
+7

All Articles