这是qt-project.org 上的一篇回帖,讲得不错所以贴过来了。

Most of what you learnt from Blanchette and Summerfield still apply to Qt 5. Signals from QML files work the same way as signals from widgets.

But anyway, I was mistaken earlier (sorry for any confusion caused). You CANcall C++ functions directly from QML. Here is an example of 2 ways to “call C++ from QML”:

MyItem.qml

main.cpp

 

Now, every time you click on your Qt Quick window, it does 2 things:

  1. Emit mySignalEmitted() which calls MyObject::mySlot1() indirectly
  2. Call MyObject::mySlot2() directly

 

Method #1 provides better encapsulation, at the cost of 1 extra line of code