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. [Solved] Emitting a QML custom type signal
Forum Updated to NodeBB v4.3 + New Features

[Solved] Emitting a QML custom type signal

Scheduled Pinned Locked Moved QML and Qt Quick
qmlsignal & slot
5 Posts 2 Posters 2.7k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    marcosbontempo
    wrote on last edited by p3c0
    #1

    Hello,

    I created an object in C++ and I registered it a library in QML:

    qmlRegisterType<DBusObject>("CustomComponents", 1, 0, "DBusObject");
    

    I can acess my C++ object as a QML component:

    import CustomComponents 1.0
    DBusObject {
        id: db
        author: "oi"
    }
    

    Now I want to pass a DBusObject component as the parameter of a QML signal:

    signal buttonSend(DBusObject db)
    

    And receive the DBusObject in C++:

    QObject::connect(rect, SIGNAL(buttonSend(DBusObject)), &sender, SLOT(RegisterAndListen(DBusObject)));
    

    But it's not working and I get the following error:

    QObject::connect: No such signal Button_QMLTYPE_29_QML_31::buttonSend(DBusObject) in ../QT Dbus enviar Objeto/sisc2/main.cpp:32
    

    Is there a way to pass a custom parameter in a QML signal? Can I only use the base types like string and int? How can I reference my DBusOBject in QML so I can use it as a signal parameter?

    Any tip will be very helpful.
    Thanks.

    p3c0P 1 Reply Last reply
    0
    • M marcosbontempo

      Hello,

      I created an object in C++ and I registered it a library in QML:

      qmlRegisterType<DBusObject>("CustomComponents", 1, 0, "DBusObject");
      

      I can acess my C++ object as a QML component:

      import CustomComponents 1.0
      DBusObject {
          id: db
          author: "oi"
      }
      

      Now I want to pass a DBusObject component as the parameter of a QML signal:

      signal buttonSend(DBusObject db)
      

      And receive the DBusObject in C++:

      QObject::connect(rect, SIGNAL(buttonSend(DBusObject)), &sender, SLOT(RegisterAndListen(DBusObject)));
      

      But it's not working and I get the following error:

      QObject::connect: No such signal Button_QMLTYPE_29_QML_31::buttonSend(DBusObject) in ../QT Dbus enviar Objeto/sisc2/main.cpp:32
      

      Is there a way to pass a custom parameter in a QML signal? Can I only use the base types like string and int? How can I reference my DBusOBject in QML so I can use it as a signal parameter?

      Any tip will be very helpful.
      Thanks.

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by p3c0
      #2

      Hi @marcosbontempo,
      The error means that the signal is not found in rect object. You need to find the exact item which contains that signal.
      Now on QML side, send the signal as usual as: buttonSend(db). Here the id of that instance is passed.
      Also I think you may need use pointer in signal connection:

      QObject::connect(rect, SIGNAL(buttonSend(DBusObject *)), &sender, SLOT(RegisterAndListen(DBusObject *)));
      

      157

      1 Reply Last reply
      0
      • M Offline
        M Offline
        marcosbontempo
        wrote on last edited by marcosbontempo
        #3

        Hi @p3c0,
        Thanks for answering! I found the item which contains the signal. It's working when I send it with strings, but still not working with my DBusObject. How do I have to declare the signal? Is the following code right?

        DBusObject {
            id: db
            author: "oi"
        }
        
        Button {
                signal buttonSend(DBusObject dbobject)
        
                objectName: "btnLogin"
                id: button1
                x: 12
                y: 39
                text: qsTr("Send")
                onClicked: button1.buttonSend(db)
            }
        

        I'm also having problems in the C++ code. I connected the QML signal with the C++ code as you said:

        QObject::connect(rect, SIGNAL(buttonSend(DBusObject *)), &sender, SLOT(RegisterAndListen(DBusObject *)));
        

        How to I have to write the class? I did like this:

        Header:

        public slots:
            void RegisterAndListen(const DBusObject object);
        

        Function:

        void send_dbus::RegisterAndListen(const DBusObject object)
        {
             ...
        }
        

        Is this right?

        Thanks!

        p3c0P 1 Reply Last reply
        0
        • M marcosbontempo

          Hi @p3c0,
          Thanks for answering! I found the item which contains the signal. It's working when I send it with strings, but still not working with my DBusObject. How do I have to declare the signal? Is the following code right?

          DBusObject {
              id: db
              author: "oi"
          }
          
          Button {
                  signal buttonSend(DBusObject dbobject)
          
                  objectName: "btnLogin"
                  id: button1
                  x: 12
                  y: 39
                  text: qsTr("Send")
                  onClicked: button1.buttonSend(db)
              }
          

          I'm also having problems in the C++ code. I connected the QML signal with the C++ code as you said:

          QObject::connect(rect, SIGNAL(buttonSend(DBusObject *)), &sender, SLOT(RegisterAndListen(DBusObject *)));
          

          How to I have to write the class? I did like this:

          Header:

          public slots:
              void RegisterAndListen(const DBusObject object);
          

          Function:

          void send_dbus::RegisterAndListen(const DBusObject object)
          {
               ...
          }
          

          Is this right?

          Thanks!

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @marcosbontempo The signal declaration is correct.
          The slot declaration and definition should use pointer

          void RegisterAndListen(DBusObject *);
          

          157

          1 Reply Last reply
          0
          • M Offline
            M Offline
            marcosbontempo
            wrote on last edited by
            #5

            Thanks @p3c0 ! Now it's working like I wanted!

            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