The following code creates a dialog. There is inconsistent behaviour happening between pressing the "Enter" key and hitting the "OK" button. When the enter key is pressed on changing a field, only that field is updated. When the OK button is pressed, both are updated (which is preferred). How can I override the Enter key to do the reasonable thing here?
What I would really like, is if the enter key sends the updated field back to the application without closing the dialog, since I would like to control something from within the dialog.
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_())