Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Can not get parameters from connected signal
QtWS25 Last Chance

Can not get parameters from connected signal

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
10 Posts 3 Posters 1.1k 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
    Cyrille de Brebisson
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Try with Connections:

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

      (Z(:^

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Cyrille de Brebisson
        wrote on last edited by
        #3

        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

        J.HilkJ 1 Reply Last reply
        0
        • C Cyrille de Brebisson

          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

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

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


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            I that enum registered with Q_ENUM, too?

            (Z(:^

            C 1 Reply Last reply
            2
            • sierdzioS sierdzio

              I that enum registered with Q_ENUM, too?

              C Offline
              C Offline
              Cyrille de Brebisson
              wrote on last edited by
              #6

              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

              sierdzioS J.HilkJ 2 Replies Last reply
              0
              • C Cyrille de Brebisson

                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

                sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

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

                (Z(:^

                1 Reply Last reply
                1
                • C Offline
                  C Offline
                  Cyrille de Brebisson
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • C Cyrille de Brebisson

                    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

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @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


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    1
                    • C Offline
                      C Offline
                      Cyrille de Brebisson
                      wrote on last edited by
                      #10

                      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

                      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