Using same enum in 2 independent different classes with Signal&Sot
-
Hello,
I have Class A, which defines the following enum:
enum MessageType{ Error, Success };
I define the same enum in another Class B (I cannot use the enum of Class A because Class A does not know anything about Class B)
Now, I want to generate a Signal in Class A with the MessageType parameter. And I defined in Class B a Slot which takes this parameter and do whatever with it:
//ClassA.h signals: void updateStatusBarSignal(QString statusMessage, ClassA::MessageType type); //ClassA.cpp emit updateStatusBarSignal("Failed", MessageType::Error); //ClassB.h public slots: void setWindowStatus(QString statusMessage, ClassB::MessageType type); //ClassB.cpp, in the constructor connect(classA, &ClassA::updateStatusBarSignal, this, &ClassB::setWindowStatus);
When I build the application, the following error occurs:
Signal and slot arguments are not compatible.
So I think, just because the two enums have the same name that does not mean that the Signal and Slot mechanism knows that this is "the same". What can I do in this case?
-
@SpaceToon said in Using same enum in 2 independent different classes with Signal&Sot:
What can I do in this case?
Use the same enum for both.
-
@Christian-Ehrlicher is there a way to tell Qt that both MessageType enums are the same type?
-
@SpaceToon said in Using same enum in 2 independent different classes with Signal&Sot:
I define the same enum in another Class B (I cannot use the enum of Class A because Class A does not know anything about Class B)
This doesn't make sense to me. The parameter type of the signal must be known to the slot, else it wouldn't know what it meant. You should at least share the
enum
of the signal, not have two separate ones. I think this is what @Christian-Ehrlicher is advising you --- "Use the same enum for both". Which will also solve your "same type" issue.If you're really determined to keep them apart, you can always pass the signal parameter as an
int
. -
@JonB said in Using same enum in 2 independent different classes with Signal&Sot:
The parameter type of the signal must be known to the slot, else it wouldn't know what it meant. You should at least share the >enum of the signal, not have two separate ones
Hey, thanks for your reply. This is actually my goal, but I think I expressed myself unclear. So, if I have the class MainWindow which sets the statusbar message and a second Class Controller which sends a signal to set the statusbar (because Controller Class is not allowed to interact with the UI). then therefore I have to define the enum in Controller Class and acces it from my MainWindow Class via Controller::MessageType ?
I don't know if I can make it clear what I actually want.
-
@SpaceToon said in Using same enum in 2 independent different classes with Signal&Sot:
is there a way to tell Qt that both MessageType enums are the same type?
no, because they are not (and this is nothing Qt specific).
Share the enum in a common header. -
@SpaceToon
If you are determined to keep the signal class and the slot class separate without sharing a commonenum
, but you are prepared to do things via a common "controller" class, you can proceed as follows:- Export the two
enum
s from their.h
files. - Import these two files into the "controller" class.
- Have the controller class place the
connect()
from the signal class to the slot class. - The call to the
connect()
attaches the signal with itsenum
to a lambda (or similar) in the controller which maps/translates the signal'senum
value to the corresponding slot'senum
value.
- Export the two