lundi 15 mai 2023

Create protected function in base class and receive slots as arguments

I have created a base and derived class. Both have some common code which differs only in the slots when I have use connect. I intend to create a common function which is protected and takes as an argument the slot function pointer.

class Base
{
protected:
virtual void createcommon(void(*)(int));

slots:
void slotBase(int);
};

class Derived
{
slots:
void slotDerived(int);
}

void Base::createcommon(void(*slotPtr)(int))
{
//some common stuff
connect(m_comboBox, QOverload<int>::of(&QComboBox::ACurrentIndexChanged), this, slotPtr);
}

void Base::xyz()
{
createcommon(&Base::slotBase); //does not work, compiler says "argument of type void (Base::*)(int) is incompatible with type void (*)(int).
}

void Derived::func()
{
createcommon(&Derived::slotDerived); //does not work, compiler says "argument of type void (Derived::*)(int) is incompatible with type void (*)(int).
}

Is it possible to do so via function pointers? Otherwise the option left is to create a second virtual function like virtual void initConnect() in both Base and Derived classes to connect appropriately.

Aucun commentaire:

Enregistrer un commentaire