Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Trouble with Connections

Trouble with Connections

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 2.6k 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.
  • J Offline
    J Offline
    Jpraccio
    wrote on last edited by
    #1

    I am trying to send a signal from C++ to QML.
    It seems I have done everything correct according to the documentation, however the signal is never received.

    I have the C++ object as a context property:

        context->setContextProperty("midiInterface", &midiInterface);
    

    have defined a signal in the MidiInterface class:

    signals:
        void updateTriggered();
    

    And here's the connection:

        Connections {
            target: midiInterface
    
            onUpdateTriggered: {
                console.log("trigger update received")
                generateNewBlossom();
            }
        }
    

    When the signal is emitted which I have verified through the debugger, the function is not called and there is no console output.

    Can't seem to find any documentation on why this may be happening.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Are you sure that syntax works when its a c++ signal ?
      The docs says
      "A Connections object creates a connection to a QML signal."

      Anyway, I might just have over complicated it using the method
      https://andrew-jones.com/blog/qml2-to-c-and-back-again-with-signals-and-slots/
      section "Emitting a signal from C++ and listening to it from QML"

      if "onUpdateTriggered:" can in fact auto bind, its much more elegant :)

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jpraccio
        wrote on last edited by
        #3

        Hey, thanks for the response,

        I got that from this doc and a few stack overflows:
        http://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html

        Right at the bottom.

        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          You are right. it does say
          "ApplicationData has a signal named dataChanged()"
          and you even followed the naming exactly.

          So i assume your code is very close to the sample except other names?

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Jpraccio
            wrote on last edited by
            #5

            Yes, just the names are different.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jpraccio
              wrote on last edited by
              #6

              Also, I can see using your method, but I have no clue how I would get to the QML object

              mrjjM 1 Reply Last reply
              0
              • J Jpraccio

                Also, I can see using your method, but I have no clue how I would get to the QML object

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Jpraccio
                Hi
                I just tested the sample project and it worked.. :)
                So there must be something a little bit off some place in your code or something else.

                Test project
                https://www.dropbox.com/s/2ixu3sx172e97rm/untitled54.zip?dl=0

                1 Reply Last reply
                0
                • 6thC6 Offline
                  6thC6 Offline
                  6thC
                  wrote on last edited by 6thC
                  #8

                  Have you registered it? For cpp instanced objects I register mine with:

                  qmlRegisterUncreatableType<T>
                  

                  http://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterUncreatableType

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    Jpraccio
                    wrote on last edited by
                    #9

                    I have it able to receive the signal if I put the connection in the top level Window qml. The qml object that needs the signal is nested a bit down in the hierarchy. Is this a know behavior?

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jpraccio
                      wrote on last edited by
                      #10

                      So it seems like nothing is received down the hierarchy from the main window. I have used the Connections inside my Window qml and it's triggered fine. Trying to pass a signal from the Window down to the Component using QML and a Connection object inside the Component fails.

                      So the components are inside a stack view. They are Component classes that contain a rectangle. The signal needs to happen within these components. Don't know if it matter, but these are not the initialItem of the StackView. I can get some code together if desired.

                      J.HilkJ 1 Reply Last reply
                      0
                      • J Jpraccio

                        So it seems like nothing is received down the hierarchy from the main window. I have used the Connections inside my Window qml and it's triggered fine. Trying to pass a signal from the Window down to the Component using QML and a Connection object inside the Component fails.

                        So the components are inside a stack view. They are Component classes that contain a rectangle. The signal needs to happen within these components. Don't know if it matter, but these are not the initialItem of the StackView. I can get some code together if desired.

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

                        @Jpraccio I think, my knowledge is limited here, that that behaviour depends heavly on how you marry your cpp class and your qml root file.

                        if for example you use qmlRegisterType<MyClass>("MyClass",1,0, "mClass"); than you can create an instande in each qml file you import myclass in, and can use Connect and signal slots there easily enough.

                        If you create it as a fixed class and register it as a rootContext() / ContextProperty than you should be able to access it everywhere in your qml files.

                        //main.cpp
                        MyClass mClass;
                        QQmlApplicationEngine engine;
                        engine.rootContext()->setContextProperty("MyClass", &mClass);
                        
                        //any qml file
                        Connections{
                            MyClass.onMySignal: console.log("SIGNAL!!!")
                        }
                        //The actual signal in MyClass is named: mySignal
                        //I believe the "on" prefix is needed
                        

                        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
                        0
                        • J Offline
                          J Offline
                          Jpraccio
                          wrote on last edited by Jpraccio
                          #12

                          I have the c++ class working at this point, I can send a signal to the window qml. Now trying to send a signal into the qml objects from the window. It seems these specific qml components just can't receive them. It looks like this:

                          // Window qml
                          Window {
                              id: mainWindow
                              signal mySig()
                              Connections {
                                   target: myCpp
                                   onAction: {
                                        // this works
                                        mySig()
                                   }
                              }
                          
                             StackView {
                                 initalItem: itemA
                                 ItemA {
                                    // this has a connection that works
                                 }
                          
                               ItemB {
                                   // this won't receive the above signal
                               }
                             }
                          }
                          
                          // ItemB qml
                          Component {
                               Rectangle {
                                   Connections {
                                       target: mainWindow
                                       onMySig: {
                                            // nothing happens here
                                            // regardles of using the cpp signal or the qml signal
                                        }
                                    }
                               }
                          
                          }
                          
                          
                          1 Reply Last reply
                          1
                          • J Offline
                            J Offline
                            Jpraccio
                            wrote on last edited by
                            #13

                            I made some changes that forced the stack view to transition and got a new error:

                            QQmlEngine: Illegal attempt to connect to Chord(0x112847200) that is in a different thread than the QML engine QQmlApplicationEngine(0x7ffeefbfe1e0.

                            Is it possible this is a threading issue of some sort? I am not intentionally spawning any threads myself

                            1 Reply Last reply
                            1
                            • J Offline
                              J Offline
                              Jpraccio
                              wrote on last edited by
                              #14

                              Solved by pushing ItemB onto the stack view.

                              1 Reply Last reply
                              1

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved