Search code examples
c++qmlsignals-slots

How to connect signal from nested qml item from c++ code?


I have a UI written in QML. The UI contains a TextEdit nested somewhere deep in the tree. I want to connect the signal onTextChanged to my c++ logic in the background. How can I access the nested signal from c++?


Solution

  • Sounds like a design issue, you shouldn't really access QML from C++, it is best to keep the interaction one way - access the exposed C++ API from QML only.

    In your case, instead of making the connection on the C++ side, you can simply install a handler for the signal in QML:

    onTextChanged : cppLogic.callCPPfoo()
    

    This is faster, easier, more flexible and can pass data even if the signal doesn't have data parameters.