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. Emit a signal with QML Items QObject * as argument
Forum Updated to NodeBB v4.3 + New Features

Emit a signal with QML Items QObject * as argument

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 3 Posters 1.4k 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by
    #1

    Hi,

    I have a multi threaded program, and would like to make an update mechanism for parameters which are in the backend but are displayed in the user interface.

    For this I thought I would make a ParameterManager in der GUI thread, and this should receive signals e.g. form CheckBoxes in the GUI. For this it would be good, if the signal coming from my QtComponents would allow to identify which ones they are (I thought about storing a map of those objects (QObject *) and their corresponding parameters in the ParameterManager.

    So In cpp I could just emit a signal with this as Parameter. How can this be done in qml?

    ODБOïO 1 Reply Last reply
    0
    • M maxwell31

      Hi,

      I have a multi threaded program, and would like to make an update mechanism for parameters which are in the backend but are displayed in the user interface.

      For this I thought I would make a ParameterManager in der GUI thread, and this should receive signals e.g. form CheckBoxes in the GUI. For this it would be good, if the signal coming from my QtComponents would allow to identify which ones they are (I thought about storing a map of those objects (QObject *) and their corresponding parameters in the ParameterManager.

      So In cpp I could just emit a signal with this as Parameter. How can this be done in qml?

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by ODБOï
      #2

      @maxwell31 said in Emit a signal with QML Items QObject * as argument:

      So In cpp I could just emit a signal with this as Parameter. How can this be done in qml?

      Item{
      signal sig(var o)
      Component.onCompleted : sig(this)
      onSig : console.log(o)
      }
      
      1 Reply Last reply
      1
      • M Offline
        M Offline
        maxwell31
        wrote on last edited by
        #3

        So this this is the same as the pointer I get when I use findChild(objectName)?

        ODБOïO 1 Reply Last reply
        0
        • M maxwell31

          So this this is the same as the pointer I get when I use findChild(objectName)?

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by ODБOï
          #4

          you can test it
          @maxwell31 said in Emit a signal with QML Items QObject * as argument:

          So this this is the same as the pointer I get when I use findChild(objectName)?

          ApplicationWindow {
              width: 800
              height: 600
              visible: true
              Item{
                  objectName: "obj"
                  signal sig(var o)
                  Component.onCompleted : sig(this)
                  onSig : utils.test(o)
              }
          }
          
          //a class
            Q_INVOKABLE void test(QQuickItem * rect){
                  qDebug() <<  rect;
              }
          
          //main.cpp 
           QList<QObject*> objects = engine.rootObjects();
             qDebug()<<objects.first()->children().at(1);
          

          see the warning at the end
          https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c

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

            Thanks! I used your code, and want to extend it to use QObject::connect like this:

            QObject * o = engine.rootObjects().value(0)->findChild<QObject*>("obj");
            CatchSignal s;
            QObject::connect(o, SIGNAL(sig(QObject *)),&s,SLOT(catchsignal(QObject*)));
            

            but get the error message:

            QObject::connect: No such signal QQuickItem_QML_0::sig(QObject *) in /home/maxwell/work/quicktest2/qobjecttest/main.cpp:24
            QObject::connect:  (sender name:   'obj')
            

            Do you know how one has to use it in connect?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maxwell31
              wrote on last edited by
              #6

              Ok, I got it. Obviously the slot and signal need to have QVariant as argument. Thanks!

              1 Reply Last reply
              0
              • GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #7

                you can do signal sig(QtObject o).

                But don't connect to a QML signal from C++.
                Just call a C++ Q_INVOKABLE/Slots from QML.

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  maxwell31
                  wrote on last edited by
                  #8

                  I didn't know that there is also the sender() function which you can use in a slot, therefore eliminating the need to pass an object.

                  What are the reasons you suggest to call a Q_INVOKABLE instead of connecting in C++?

                  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