Как из одного диалогового окна QML передать содержимое поля в соответствующее поле вызвавшее его?

import QtQuick 2.7
import QtQuick.Window 2.14
import QtQuick.Controls 2.0
import QtQuick.Dialogs 1.3

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    TextField {
        id: _firstTextField
        width: parent
        height: 60
        anchors.top: parent

        Rectangle {
            id: _firstButton
            width: 40
            height: 60
            color: "black"
            anchors.right: _firstTextField.right

            MouseArea {
                anchors.fill: parent
                onClicked: _dialog.open()

            }
        }
    }

    TextField {
        id: _secondTextField
        width: parent
        height: 60
        anchors.top: _firstTextField.bottom

        Rectangle {
            id: _secondButton
            width: 40
            height: 60
            color: "red"
            anchors.right: _secondTextField.right

            MouseArea {
                anchors.fill: parent
                onClicked: _dialog.open()

            }
        }
    }

    Dialog {
        id: _dialog

        TextField {
            id: _dialogTextField
            width: 200
            height: 60
        }

        onAccepted: {
            _secondTextField.text = _dialogTextField.text
        }
    }
}

При нажатии кнопки _firstButton вызывается диалог, после ввода строки в диалоге и нажатия кнопки "ок", строка должна быть отображена в _firstTextField, при нажатии кнопки _secondButton текст из диалога возвращается в _secondTextField. Как в элементе Dialog, в поле onAccepted динамически менять левую часть выражения - _secondTextField.text, в зависимости от вызвавшего диалог элемента.


Ответы (1 шт):

Автор решения: Andrey Epifantsev

Можно например, сделать так:

Dialog {
    id: _dialog
    property TextField destination: null

    TextField {
        id: _dialogTextField
        width: 200
        height: 60
    }

    function openWithDestination(dest) {
        destination = dest
        open()
    }

    onAccepted: {
        if (destination)
            destination.text = _dialogTextField.text
    }
}

И вместо

onClicked: _dialog.open()

вызывать

onClicked: _dialog.openWithDestination(_firstTextField)

или

onClicked: _dialog.openWithDestination(_secondTextField)
→ Ссылка