How to capture a signal in QML?

后端 未结 4 1947
南方客
南方客 2021-02-08 04:50

How I can send s signal from one qml component to another?

Below is an example:

Rectangle {
    id: main
    width: 360; height: 360
    signal clicked()         


        
4条回答
  •  猫巷女王i
    2021-02-08 05:32

    QML

    Button{
      id: btn_add_pq
      text: "Add"
      onClicked: {
        var component = Qt.createComponent("add_pq.qml")
        var dialog    = component.createObject(root)
        dialog.open()
    
        dialog.accepted.connect(function(){
          console.log("ID     :" + window.in_id)
          console.log("Name   :" + window.in_name)
          console.log("Comment:" + window.in_comment)
        })
      }
    }
    

    add_pq.qml

    Dialog {
      id:dialog
      ...
      property alias in_id: txtID.text
      property alias in_comment: txtComment.text
      property alias in_name: txtName.text
      ...
      contentItem: GridLayout{
        ...
        TextField{
          id: txtComment
          Layout.alignment: Qt.AlignRight
        }
        Button{
          Layout.columnSpan: 2
          text: "Add"
          Layout.alignment: Qt.AlignRight
          onClicked: {
            dialog.click(StandardButton.Save)
          }
        }
      }
    

提交回复
热议问题