I'm trying to connect the signal currentIndexChanged(int)
from QComboBox
to a slot from my class that receive an enum, like Foo::mySlot(EnumFoo)
.
I know that all these ways DON'T work:
connect(cb, &QComboBox::currentIndexChanged, foo, &Foo::mySlot);
connect(cb, static_cast<void (QComboBox::*)(EnumFoo)>(&QComboBox::currentIndexChanged), foo, &Foo::mySlot);
connect(cb, &QComboBox::currentIndexChanged, foo, static_cast<void (Foo::*)(int)>(&Foo::mySlot));
because in C/C++, int
never cast implicitly to an enum type. The opposite is true, I think if my signal has an enum parameter, I can connect it to a slot with int parameter without problem.
I know how to solve this question using lambda function:
connect(cb, &QComboBox::currentIndexChanged, [=](int i){ foo.mySlot(static_cast<EnumFoo>(i)); });
Is there any way to solve this problem WITHOUT lambda function?
EDIT: Using the solution I proposed, I'm connecting Foo::mySlot this way.
LambdaWrapper *lw = new LambdaWrapper;
lw->connect(cb, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [=](int i){ foo.mySlot(static_cast<EnumFoo>(i)); }, foo);
and I don't have to worry about disconnection stuff anymore. Just have to manage lw
lifespan.
static_cast
and does exactly the same as your lambda.... Are you offended by the lambda (I'm sure it didn't mean it that way), are you used to not-so-strongly types languages, or do you have the misconception that this is going to ruin ~performance~? – rubenvbQMetaObject::Connection
before destruct the QComboBox or the Foo class usingQObject::disconnect(const QMetaObject::Connection&)
overloaded member. Or if we can just ignore it. If I can ignore this disconnect stuff, no problem using lambdas. On the other hand if I have to deal with it, maybe I should use a wrapper slot as pointed by you. – Vinícius A. Jorge