Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using same enum in 2 independent different classes with Signal&Sot
QtWS25 Last Chance

Using same enum in 2 independent different classes with Signal&Sot

Scheduled Pinned Locked Moved Unsolved General and Desktop
signals & slotsenum
7 Posts 3 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SpaceToon
    wrote on 16 May 2020, 16:47 last edited by SpaceToon
    #1

    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?

    J 1 Reply Last reply 16 May 2020, 17:40
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 16 May 2020, 16:57 last edited by
      #2

      @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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply 16 May 2020, 17:28
      1
      • C Christian Ehrlicher
        16 May 2020, 16:57

        @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.

        S Offline
        S Offline
        SpaceToon
        wrote on 16 May 2020, 17:28 last edited by SpaceToon
        #3

        @Christian-Ehrlicher is there a way to tell Qt that both MessageType enums are the same type?

        C 1 Reply Last reply 16 May 2020, 19:08
        0
        • S SpaceToon
          16 May 2020, 16:47

          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?

          J Offline
          J Offline
          JonB
          wrote on 16 May 2020, 17:40 last edited by JonB
          #4

          @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.

          S 1 Reply Last reply 16 May 2020, 18:14
          1
          • J JonB
            16 May 2020, 17:40

            @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.

            S Offline
            S Offline
            SpaceToon
            wrote on 16 May 2020, 18:14 last edited by
            #5

            @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.

            J 1 Reply Last reply 18 May 2020, 07:40
            0
            • S SpaceToon
              16 May 2020, 17:28

              @Christian-Ehrlicher is there a way to tell Qt that both MessageType enums are the same type?

              C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 16 May 2020, 19:08 last edited by
              #6

              @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.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • S SpaceToon
                16 May 2020, 18:14

                @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.

                J Offline
                J Offline
                JonB
                wrote on 18 May 2020, 07:40 last edited by
                #7

                @SpaceToon
                If you are determined to keep the signal class and the slot class separate without sharing a common enum, but you are prepared to do things via a common "controller" class, you can proceed as follows:

                • Export the two enums 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 its enum to a lambda (or similar) in the controller which maps/translates the signal's enum value to the corresponding slot's enum value.
                1 Reply Last reply
                0

                7/7

                18 May 2020, 07:40

                • Login

                • Login or register to search.
                7 out of 7
                • First post
                  7/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved