信号创建

  1. signal testSig(string s, int value)

槽函数

  1. function func(ss, ii){
  2. console.log("s: ",ss, " i: ", ii)
  3. }

信号绑定

  1. Component.onCompleted: {
  2. testSig.connect(func) //绑定信号处理函数
  3. }

信号触发

  1. Button{
  2. width: 50
  3. height: 20
  4. onClicked:
  5. {
  6. //触发信号
  7. testSig("zhangsan", 22)
  8. }
  9. }
  1. //定义一个信号
  2. signal testSig(string s, int value)
  3. function func(ss, ii){
  4. console.log("s: ",ss, " i: ", ii)
  5. }
  6. Button{
  7. width: 50
  8. height: 20
  9. onClicked:
  10. {
  11. //触发信号
  12. testSig("zhangsan", 22)
  13. }
  14. }
  15. Component.onCompleted: {
  16. testSig.connect(func) //绑定信号处理函数
  17. }

image.png
使用Connections组件绑定信号与槽。

Connections

属性

  1. enabled : bool
  2. ignoreUnknownSignals : bool //target无改信号的时候忽略掉function绑定无效信号导致的编译警告
  3. target : Object //信号发出者

替换绑定方式1:

  1. Connections{
  2. target:root
  3. onTestSig:{
  4. console.log(s," ", value) //参数名称不直接
  5. }
  6. }

on+信号首字母大写的方式绑定信号槽不推荐,这主要是qml自带槽的写法。

替换绑定方式2:

function on+首字母大写信号名+括号信号参数名称(可以自己指定), 然后槽具体实现

  1. Connections{
  2. target:root
  3. function onTestSig(str, ivalue)
  4. {
  5. console.log("onTestSig func ", str, " ", ivalue)
  6. }
  7. }

自定义组件信号槽的应用:

  1. import QtQuick 2.0
  2. import QtQuick.Controls 2.15
  3. Rectangle {
  4. width: 200
  5. height: 100
  6. anchors.centerIn: parent
  7. property Component com1 //不给默认值
  8. property Component com2
  9. border.color: "red"
  10. Loader{
  11. id: loader1
  12. sourceComponent: com1
  13. anchors.bottom: parent.bottom
  14. anchors.bottomMargin: 20
  15. anchors.right: parent.right
  16. anchors.rightMargin: 100
  17. Connections{
  18. target: loader1.item
  19. ignoreUnknownSignals: true //当该信号不存在时忽略掉
  20. function onBtnSig(value)
  21. {
  22. console.log("top value ", value)
  23. }
  24. function onRightBtnPressed()
  25. {
  26. loader2.item.focus = true
  27. loader2.item.forceActiveFocus()
  28. console.log("onRightBtnPressed")
  29. }
  30. }
  31. Component.onCompleted: {
  32. loader1.item.focus = true
  33. loader1.item.forceActiveFocus()
  34. }
  35. }
  36. Loader{
  37. id: loader2
  38. sourceComponent: com2
  39. anchors.bottom: parent.bottom
  40. anchors.bottomMargin: 20
  41. anchors.right: parent.right
  42. anchors.rightMargin: 20
  43. Connections{
  44. target: loader2.item
  45. ignoreUnknownSignals: true
  46. function onBtnSig(value)
  47. {
  48. console.log("bottom value ", value)
  49. }
  50. function onLeftBtnPressed()
  51. {
  52. loader1.item.focus = true
  53. loader1.item.forceActiveFocus()
  54. console.log("onLeftBtnPressed")
  55. }
  56. }
  57. }
  58. }
  1. Component{
  2. id: com
  3. Button{
  4. id: btn1
  5. width:50
  6. height: 20
  7. background: Rectangle{
  8. border.color: btn1.activeFocus ? "red":"green" //activeFocus体现了唯一性
  9. border.width: 2
  10. }
  11. //// signal btnSig(int v)
  12. // onClicked: {
  13. //// btnSig(22)
  14. // }
  15. signal leftBtnPressed()
  16. Keys.onLeftPressed: {
  17. leftBtnPressed()
  18. console.log("btn onLeftPressed")
  19. }
  20. signal rightBtnPressed()
  21. Keys.onRightPressed: {
  22. rightBtnPressed()
  23. console.log("btn onRightPressed")
  24. }
  25. }
  26. }
  27. MyComponent{
  28. com1:com
  29. com2:com
  30. }

image.png
自定义组件中使用Loader进行自定义组件的加载,通过Loader属性item获取到自定义的组件并赋给Connections的target属性,为了防止未定义的方法的绑定,加上ignoreUnknownSignals:true防止编译器报错,正式项目中可不加不然bug不好找。