Python 如何在QML对话框中更改Enter键的行为?

Python 如何在QML对话框中更改Enter键的行为?,python,dialog,qml,pyqt5,pyside2,Python,Dialog,Qml,Pyqt5,Pyside2,下面的代码创建一个对话框。按下“回车”键和按下“确定”按钮之间出现不一致的行为。在更改字段时按enter键时,仅更新该字段。按下OK(确定)按钮时,两者都会更新(这是首选)。我如何覆盖Enter键来执行合理的操作 我真正想要的是,如果enter键在不关闭对话框的情况下将更新的字段发送回应用程序,因为我希望从对话框中控制某些内容 view.qml import QtQuick 2.0 import QtQuick.Layouts 1.12 import QtQuick.Controls 2.12

下面的代码创建一个对话框。按下“回车”键和按下“确定”按钮之间出现不一致的行为。在更改字段时按enter键时,仅更新该字段。按下OK(确定)按钮时,两者都会更新(这是首选)。我如何覆盖Enter键来执行合理的操作

我真正想要的是,如果enter键在不关闭对话框的情况下将更新的字段发送回应用程序,因为我希望从对话框中控制某些内容

view.qml

import QtQuick 2.0
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
import QtQuick.Window 2.12
import QtQuick.Dialogs 1.3


Item {
    Dialog {
        id: thedialog
        ColumnLayout {
            TextField {
                id: numberField
                onAccepted: {
                    backend.number = text
                }
            }
            TextField {
                id: textField
                onAccepted: {
                    backend.text = text
                }
            }
        }

        onButtonClicked: {
            backend.number = numberField.text
            backend.text = textField.text
        }

    }
    Button {
        text: "Show Dialog"
        onClicked: thedialog.open()
    }
}
main.py

import sys
from PySide2.QtCore import QObject, Signal, Property
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtQuickWidgets import QQuickWidget

class Backend(QObject):

    def __init__(self):
        QObject.__init__(self)
        self._number = 0
        self._text = ""

    def getNumber(self):
        return self._number

    def setNumber(self, number):
        print(f"Setting number to: {number}")
        self._number = number
        self.notifyNumber.emit()

    notifyNumber = Signal()

    number = Property(float, getNumber, setNumber, notify=notifyNumber)

    def getText(self):
        return self._text

    def setText(self, text):
        print(f"Setting text to: {text}")
        self._text = text
        self.notifyText.emit()

    notifyText = Signal()

    text = Property(str, getText, setText, notify=notifyText)



if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = QMainWindow()

    quick = QQuickWidget()
    backend = Backend()
    quick.rootContext().setContextProperty("backend", backend)
    quick.setSource("view.qml")

    window.setCentralWidget(quick)
    window.show()

    sys.exit(app.exec_())
使用:


谢谢我注意到这一点很好(并不能完全阻止Enter键,但在文本字段处于焦点时有效),但仅适用于键盘中央的Enter键。numpad上的Enter键也可以这样做吗?我认为这应该只是为信号添加另一个信号处理程序的问题:
Keys.one解释:Dialog.doSomeStuff,然后再关闭Dialog()
谢谢。我会尽我所能将这一条标记为最完整的,尽可能接近我所追求的。如果有人知道当我按enter键而文本字段未处于焦点时,如何阻止它关闭对话框(因此在对话框本身上覆盖enter键),那也太棒了。对我来说,当我按enter键而文本字段未处于焦点时,对话框不会关闭。哦,我使用的是Qt Quick Controls 2的对话框,是的。:)Qt Quick Dialogs中的对话框需要单独导入,但较新的对话框不需要。
import QtQuick 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.14

ApplicationWindow {
    width: 800
    height: 600
    visible: true

    QtObject {
        id: backend
        property real number
        property string text
    }

    Dialog {
        id: thedialog

        function doSomeStuffBeforeClosingTheDialog() {
            backend.number = parseFloat(numberField.text)
            backend.text = textField.text
            // Remove this if you do not want the dialog to close
            accept()
        }

        ColumnLayout {
            TextField {
                id: numberField
                onAccepted: backend.number = text

                Keys.onReturnPressed: thedialog.doSomeStuffBeforeClosingTheDialog()
            }
            TextField {
                id: textField
                onAccepted: backend.text = text

                Keys.onReturnPressed: thedialog.doSomeStuffBeforeClosingTheDialog()
            }
        }
    }
    Button {
        text: "Show Dialog"
        onClicked: thedialog.open()
    }
}