Can not get parameters from connected signal



  • Hello,

    I have a C++ object, dutifully registered as a singleton with the qml system.
    This object has a newDevice signal with 2 parameters (the second one being a Qml registered enum):

    class obj : public QObject {
    Q_OBJECT
    Q_SIGNALS:
    void newDevice(int linkId, enumType type);

    in my main qml code, I connect a function to the onNewDevice signal like so:

        Component.onCompleted: {
            HPACModel.onNewDevice.connect(function(linkId, type) { console.log("new device id:", linkId, "type:", type);  }
    }
    

    However, it does not seem to do what I want. When I emit a newDevice(1,1) for example, my function is called, but it displays in the console:
    qml: new device id: undefined type: undefined

    Can you tell me what I am doing wrong?

    Thanks


  • Moderators

    Try with Connections:

    Connections {
      target: HPACModel
    
      onNewDevice: {
        console.log("new device id:", linkId, "type:", type)
      }
    }
    


  • Hello,

    Nope, sorry, but I still get the same result :-(

    Strangely enough, if I change the type from my current enumType to an int, then both my and your versions do work... So it looks like it has something to do with the enum... Any ideas?

    Cyrille



  • @Cyrille-de-Brebisson
    did you register your enum via qmlRegisterUncreatableType ?


  • Moderators

    I that enum registered with Q_ENUM, too?



  • Hello,

    Here is the "real" code...
    The enum with the Q_ENUM

    class HPACDevices : public QObject {
    Q_OBJECT
    public:
    HPACDevices(QObject *parent=nullptr): QObject(parent) {}
    enum HPACDeviceTypes
    {
    Unknown= 0,
    Pinky,
    Brain,
    Kazar
    };
    Q_ENUMS(HPACDeviceTypes)
    };

    The registration of the class in main.cpp reads as follow...
    qmlRegisterType<HPACDevices>("HPACDevices", 1, 0, "HPACDevices");

    I do not have any calls to qmlRegisterUncreatableType. Where should that go and what parameters should it have?

    Thanks


  • Moderators

    @Cyrille-de-Brebisson said in Can not get parameters from connected signal:

    Q_ENUMS(HPACDeviceTypes)

    Q_ENUM, not Q_ENUMS. The difference is small but very important.



  • Hello,

    changing from Q_ENUMS to Q_ENUM did change some things (some of the other displays that I had changed from 2 to HPACDeviceTypes(Brain)), however my core issue, the fact that I can not read the parameters in my Qml signal handler still persists :-(

    (Ps: I have switch to the "connection" scheme)...

    Cyrille



  • @Cyrille-de-Brebisson said in Can not get parameters from connected signal:

    I do not have any calls to qmlRegisterUncreatableType. Where should that go and what parameters should it have?

    Thanks

    something along this

    qmlRegisterUncreatableType<myClass>("CppEnums",1,0,"Enums","Enum is not a type");
    
    

    inside your qml file you'll have to import CppEnums to have access to the enums

    import CppEnums 1.0
    
    ...
    Enums.EnumName
    

    also you Enums need to start with a capital letter to work in qml



  • Hello,

    Nope, using qmlRegisterUncreatableType does not help. The symptoms are the same with the parameters being undef.
    Note, with the qmlRegisterType I have no issues accessing the enum in Qml. The problem is that the arguements that are passed to my signal handler are undefined (as per Qml) if/when the C++ side of the signal handler defines them as my enum type. However, they are "ok" if they are integers.

    Is a signal not able to use anything else than "basic types"?

    Cyrille


Log in to reply
 

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