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



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



  • 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);
            }
        );
    


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



  • Excellent! Thanks for letting us know.

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


  • Moderators

    This post is deleted!


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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.