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. Connect signal with int param to a slot with enum param?
QtWS25 Last Chance

Connect signal with int param to a slot with enum param?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.7k 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.
  • C Offline
    C Offline
    Calicoder
    wrote on last edited by
    #1

    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.

    Pl45m4P JonBJ 2 Replies Last reply
    0
    • C Calicoder

      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.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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 a enum type, which is not the case here. Therefore you got this parameter mismatch error)
      Change it to int and "translate" it inside your setValue function (slot).


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      C S 2 Replies Last reply
      1
      • C Calicoder

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @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)) or SLOT(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()?)

        Pl45m4P 1 Reply Last reply
        3
        • JonBJ JonB

          @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)) or SLOT(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()?)

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @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 AFAIK int and enum are not compatible as such. You could use ints or register your enum and pass the enum value around.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          2
          • Pl45m4P Pl45m4

            @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 a enum type, which is not the case here. Therefore you got this parameter mismatch error)
            Change it to int and "translate" it inside your setValue function (slot).

            C Offline
            C Offline
            Calicoder
            wrote on last edited by
            #5

            @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 a enum type, which is not the case here. Therefore you got this parameter mismatch error)
            Change it to int and "translate" it inside your setValue function (slot).

            This is what I tried and it works perfectly, Thanks so much!

            JonBJ 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @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 a enum type, which is not the case here. Therefore you got this parameter mismatch error)
              Change it to int and "translate" it inside your setValue function (slot).

              S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #6

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

              Pl45m4P 1 Reply Last reply
              0
              • C Calicoder

                @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 a enum type, which is not the case here. Therefore you got this parameter mismatch error)
                Change it to int and "translate" it inside your setValue function (slot).

                This is what I tried and it works perfectly, Thanks so much!

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

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

                1 Reply Last reply
                1
                • S SimonSchroeder

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

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #8

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

                  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.


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  S 1 Reply Last reply
                  0
                  • Pl45m4P Pl45m4

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

                    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.

                    S Offline
                    S Offline
                    SimonSchroeder
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    1

                    • Login

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