Trouble with Connections



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


  • Qt Champions 2016

    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 :)



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


  • Qt Champions 2016

    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?



  • Yes, just the names are different.



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


  • Qt Champions 2016

    @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



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

    qmlRegisterUncreatableType<T>
    

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



  • 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?



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



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


  • 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
                  }
              }
         }
    
    }
    
    


  • 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



  • Solved by pushing ItemB onto the stack view.


Log in to reply
 

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