Connect signal with int param to a slot with enum param?
-
Howdy fellow QT'ers, wondering if anyone has any suggestions on how to solve this? I've been searching since yesterday and don't see any useful answers.
Problem: I have a connect that uses a signal with an int parameter connected to a slot with an enum parameter.
connect(classOne, &ClassOne::getValue(int), this, &ClassTwo::setValue(enum));
I'm getting build errors that the signal and slot arguments are not compatible. Aren't enums integer values? Thinking I can take that signal int and static cast it as an enum maybe?
Thanks for the suggestions, much appreciated.
-
@Calicoder said in Connect signal with int param to a slot with enum param?:
Aren't enums integer values?
Yes, but you wrote
enum
.
(It expects to pass aenum
type, which is not the case here. Therefore you got this parameter mismatch error)
Change it toint
and "translate" it inside yoursetValue
function (slot). -
@Calicoder said in Connect signal with int param to a slot with enum param?:
connect(classOne, &ClassOne::getValue(int), this, &ClassTwo::setValue(enum));
You cannot write the functions like this, with parentheses and parameter types. (At least, not to the best of my C++ knowledge.) What you have is more like a mix of new-style signal/slot connects with old-style ones, where you could write
SIGNAL(getValue(int))
orSLOT(setValue(enum))
. Don't you find you have to write:connect(classOne, &ClassOne::getValue, this, &ClassTwo::setValue);
?
If I am wrong, and you can write what you did, I should like a C++ expert to comment and point me to a reference so I can read up, please! (And if you can specify parameter types, why does Qt need
QOverload::of()
?) -
@JonB said in Connect signal with int param to a slot with enum param?:
You cannot write the functions like this, with parentheses and parameter types. (At least, not to the best of my C++ knowledge.) What you have is more like a mix of new-style signal/slot connects with old-style ones, where you could write SIGNAL(getValue(int)) or SLOT(setValue(enum))
Didn't even realized it :)
@Calicoder
But AFAIKint
andenum
are not compatible as such. You could use ints or register your enum and pass the enum value around. -
@Pl45m4 said in Connect signal with int param to a slot with enum param?:
@Calicoder said in Connect signal with int param to a slot with enum param?:
Aren't enums integer values?
Yes, but you wrote
enum
.
(It expects to pass aenum
type, which is not the case here. Therefore you got this parameter mismatch error)
Change it toint
and "translate" it inside yoursetValue
function (slot).This is what I tried and it works perfectly, Thanks so much!
-
@Pl45m4 said in Connect signal with int param to a slot with enum param?:
Change it to int and "translate" it inside your setValue function (slot).
I would advocate not to change the parameter type of
setValue
. If it expects an enum that is fine and should not be changed. If you want to connect the signal with the slot, there is a simple way: Use a lambda which does the conversion and callssetValue
. -
@Calicoder said in Connect signal with int param to a slot with enum param?:
This is what I tried and it works perfectly,
For anyone else coming to read this thread of yours: You have said the code you show:
connect(classOne, &ClassOne::getValue(int), this, &ClassTwo::setValue(enum));
"works perfectly", despite my suggestion that this will not compile and
connect(classOne, &ClassOne::getValue, this, &ClassTwo::setValue);
is needed. Is that what you are claiming, for the benefit of other readers?
-
@SimonSchroeder said in Connect signal with int param to a slot with enum param?:
@Pl45m4 said in Connect signal with int param to a slot with enum param?:
Change it to int and "translate" it inside your setValue function (slot).
I would advocate not to change the parameter type of
setValue
. If it expects an enum that is fine and should not be changed. If you want to connect the signal with the slot, there is a simple way: Use a lambda which does the conversion and callssetValue
.Yeah if
setValue
is a given function of some class. But here it is @Calicoder 's own function, I guess. So you can change it as needed :)Anyway, it's always a good idea to register your enums if you plan to send enum values around using Qt signals and slot. Then you can simply translate the
int
to the correct enum string using the metatype. -
@Pl45m4 said in Connect signal with int param to a slot with enum param?:
Yeah if setValue is a given function of some class. But here it is @Calicoder 's own function, I guess. So you can change it as needed :)
Just because you can change it does not mean you should. C++ has strong types. And this helps to catch a lot of errors at compile time. That is why I suggest to use the correct type instead of cheap tricks. Though, that is my personal choice.