【问题标题】:How to change behaviour of Enter key in QML Dialog?如何更改 QML 对话框中 Enter 键的行为?
【发布时间】:2020-08-29 07:25:30
【问题描述】:

以下代码创建一个对话框。在按下“Enter”键和点击“OK”按钮之间发生了不一致的行为。在更改字段时按下回车键时,仅更新该字段。当按下 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_())

【问题讨论】:

    标签: python dialog qml pyqt5 pyside2


    【解决方案1】:

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

    【讨论】:

    • 谢谢!我注意到这很好用(不会完全阻止 Enter 键,但在文本字段处于焦点时有效),但仅适用于键盘中心的 Enter 键。小键盘上的回车键也可以吗?
    • 我认为应该只是为enterPressed 信号添加另一个信号处理程序:Keys.onEnterPressed: thedialog.doSomeStuffBeforeClosingTheDialog()
    • 谢谢。我会将此标记为完整的,因为我认为它可能尽可能接近我所追求的。如果有人知道当我按 Enter 并且 TextField 未处于焦点时如何阻止它关闭对话框(因此覆盖对话框本身的 enter 键),那也很棒。
    • 对我来说,当我在没有焦点的文本字段的情况下按 Enter 时,对话框不会关闭。
    • 哦,我正在使用 Qt Quick Controls 2 中的对话框,是的。 :) Qt Quick Dialogs 中的对话框需要单独导入,但较新的不需要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 2010-10-27
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    相关资源
    最近更新 更多