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. How far can I go with casting when it comes to the new way how Signal & Slots are connected?
Forum Update on Monday, May 27th 2025

How far can I go with casting when it comes to the new way how Signal & Slots are connected?

Scheduled Pinned Locked Moved Solved General and Desktop
7 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
    c64zottel
    wrote on last edited by
    #1

    I am trying to get this signal:

    signals:
        void joystickConnected( JoystickDevice * joystick, JoystickDeviceConfigurator * configurator );
    

    connected with this slot:

    public slots:
        void newInputDeviceDetected( InputDevice * dev, InputDeviceConfigurator * configurator );
    

    JoystickDevice inherits InputDevice publicly and the same goes for *DeviceConfigurator.

    When try:

    connect(
            & JM_instance,
              static_cast< void (JoystickManager:: *) (InputDevice *, InputDeviceConfigurator *) >
              ( & JoystickManager::joystickConnected ),
              d,           & UserInputManagement_Private::newInputDeviceDetected
        );
    

    The compiler gives me the finger with:

    error: invalid static_cast from type ‘void (JoystickManager::*)(JoystickDevice*, JoystickDeviceConfigurator*)’ to type ‘void (JoystickManager::*)(InputDevice*, InputDeviceConfigurator*)’
               static_cast< void (JoystickManager:: *) (InputDevice *, InputDeviceConfigurator *)>(& JoystickManager::joystickConnected),
    

    So, I wonder if it is even possible what am I trying to do?

    1 Reply Last reply
    0
    • 6thC6 Offline
      6thC6 Offline
      6thC
      wrote on last edited by
      #2

      I'd say you can go quite far.

      https://wiki.qt.io/New_Signal_Slot_Syntax

      Completely untested but maybe a lambda connect would *(or may not) help:

      QObject::connect(&JM_instance, &JoystickManager::joystickConnected,
          [=]( InputDevice *device, InputDeviceConfigurator *config){
             doSomething(device);
      doAnotherThing(config);
              }
          );
      
      C 1 Reply Last reply
      3
      • 6thC6 6thC

        I'd say you can go quite far.

        https://wiki.qt.io/New_Signal_Slot_Syntax

        Completely untested but maybe a lambda connect would *(or may not) help:

        QObject::connect(&JM_instance, &JoystickManager::joystickConnected,
            [=]( InputDevice *device, InputDeviceConfigurator *config){
               doSomething(device);
        doAnotherThing(config);
                }
            );
        
        C Offline
        C Offline
        c64zottel
        wrote on last edited by
        #3

        @6thC This workaround works nice.

        I ended up with this:

        connect(
                & JM_instance,
                & JoystickManager::joystickConnected,
                  [=] ( InputDevice * device, InputDeviceConfigurator * configurator ) {
                        d->newInputDeviceDetected(  device,  configurator );
                }
            );
        

        If no better answer comes up in the next days I will mark your suggestion as solution.

        1 Reply Last reply
        2
        • 6thC6 Offline
          6thC6 Offline
          6thC
          wrote on last edited by
          #4

          Excellent! Thanks for letting us know.

          Glad that got you moving again, also, no casting required :-)

          1 Reply Last reply
          1
          • EddyE Offline
            EddyE Offline
            Eddy
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            2
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              I don't agree with the answers above, you should not use a lambda here. the problem is just that the compiler does not know JoystickDevice is a InputDevice or JoystickDeviceConfigurator is a InputDeviceConfigurator or both. basically you just have to #include the headers with JoystickDevice and JoystickDeviceConfigurator definition.

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              C 1 Reply Last reply
              1
              • VRoninV VRonin

                I don't agree with the answers above, you should not use a lambda here. the problem is just that the compiler does not know JoystickDevice is a InputDevice or JoystickDeviceConfigurator is a InputDeviceConfigurator or both. basically you just have to #include the headers with JoystickDevice and JoystickDeviceConfigurator definition.

                C Offline
                C Offline
                c64zottel
                wrote on last edited by
                #7

                @VRonin Unfortunately the code has changed in the meantime.

                But I reconstructed it: different names, same story.
                It still does not work:

                #include "userinputmanagement.h"
                #include "userinputmanagement_private.h"
                #include "joystickdeviceconfigurator.h"
                #include "inputdeviceconfiguratorgate.h"
                #include "joystickmanager.h"
                #include "joystickdevice.h"
                #include "inputdevice.h"
                
                    connect(
                        & JM_instance,
                          static_cast< void (JoystickManager:: *) (InputDevice *, InputDeviceConfiguratorGate *) >
                                          ( & JoystickManager::joystickConnected ),
                          d,
                        & UserInputManagement_Private::newInputDeviceDetected
                        );
                
                

                From JoystickManager (JM_instance) the signal:

                signals:
                    void joystickConnected( JoystickDevice *, JoystickDeviceConfigurator * );
                

                The receiving slot in UserInputManagement_Private:

                public slots:
                    void newInputDeviceDetected( InputDevice * dev, InputDeviceConfiguratorGate * configurator );
                

                Inheritance structure:

                class JoystickDeviceConfigurator : public InputDeviceConfiguratorGate
                class JoystickDevice             : public InputDevice
                

                And the error:

                error: invalid static_cast from type ‘void (JoystickManager::*)(JoystickDevice*, JoystickDeviceConfigurator*)’ to type ‘void (JoystickManager::*)(InputDevice*, InputDeviceConfiguratorGate*)’
                                           ( & JoystickManager::joystickConnected ),
                

                Any further ideas?

                1 Reply Last reply
                0

                • Login

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