python - 如何更改 QML 对话框中 Enter 键的行为?

标签 python dialog qml pyqt5 pyside2

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

我真正想要的是,如果回车键将更新的字段发送回应用程序而不关闭对话框,因为我想从对话框中控制某些内容。

查看.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_())

最佳答案

使用attached Keys API :

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()
    }
}

关于python - 如何更改 QML 对话框中 Enter 键的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61768926/

相关文章:

javascript - 无法让 jQuery 切换脚本在 jQuery 对话框内运行

qt - QML 键盘快捷键会干扰 Key OnPressed 事件

qml - 文本类型对齐

image - QML如何像MacOSX dock的背景货架图像一样旋转图像

python - 尝试在 Django 中创建缩略图时获取无法识别图像文件

facebook - 必须使用 session key 调用 Iframe 对话框

python - SymPy:如何在 Google Colab 的一个单元格中输出多个 LaTex 方程?

android - 如何访问AlertDialog.Builder构建的AlertDialog消息?

python - 优化多个for循环

python - 带条件的随机数生成器 - Python