如何 return TextEdit QML 的文本值

How to return the text value of a TextEdit QML

我有 2 个文件,一个 main.qml(保存 ui 的所有数据)和一个 main.py(启动 window 并将控制逻辑)。我不知道如何找到文本输入的值。

Ui 截图

main.py

import os
from pathlib import Path
import sys
import pideck_query
import keyboard
import threading

from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine

#closes window
def quit():
    sys.exit(app.exec_())
def UserInput():
    while True:
        if keyboard.is_pressed('enter'):
            print()#i want this to print the text that the user has entered
            #pideck_query.ManualEnter()
            break
#creates window and calls to check for old
if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.load(os.fspath(Path(__file__).resolve().parent / "main.qml"))
    pideck_query.CheckForOld()
    x = threading.Thread(target=UserInput)
    x.start()
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

main.qml

import QtQuick 2.14
import QtQuick.Window 2.14

Window {
    id: window
    width: 1920
    height: 1080
    visible: true
    color: "#39393a"
    title: qsTr("PiDeck")


    Text {
        id: dropshadow
        text: qsTr("Searching For Pideck Device...")
        anchors.left: title_text.right
        anchors.top: title_text.bottom
        font.pixelSize: 120
        anchors.leftMargin: -1806
        anchors.topMargin: -131
        font.weight: Font.ExtraBold
        font.family: "Tahoma"
    }

    Text {
        id: title_text
        color: "#ffffff"
        text: qsTr("Searching For Pideck Device...")
        anchors.left: parent.left
        anchors.top: parent.top
        font.pixelSize: 120
        anchors.leftMargin: 53
        anchors.topMargin: 40
        font.weight: Font.ExtraBold
        font.family: "Tahoma"
    }

    TextInput {
        id: ip_input
        width: 830
        height: 121
        color: "#ffffff"
        text: qsTr("Or enter ip manually")
        anchors.left: title_text.right
        anchors.top: title_text.bottom
        font.pixelSize: 80
        anchors.leftMargin: -1813
        anchors.topMargin: 188
        font.weight: Font.ExtraBold
        font.family: "Tahoma"
    }
}

/*##^##
Designer {
    D{i:0;formeditorZoom:0.33}D{i:1}D{i:2}D{i:3}
}
##^##*/

不需要使用键盘或外部库来获取 QML 文本。解决方法是创建一个QObject,通过Slot暴露一个方法,调用Keys.onReturnPressed时必须调用那个方法。

import os
from pathlib import Path
import sys


from PySide2.QtCore import QCoreApplication, QObject, Qt, QUrl, Slot
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine

CURRENT_DIRECTORY = Path(__file__).resolve().parent


class Backend(QObject):
    @Slot(str)
    def foo(self, text):
        print("text")


def main():
    app = QGuiApplication(sys.argv)

    backend = Backend(app)

    engine = QQmlApplicationEngine()

    engine.rootContext().setContextProperty("backend", backend)

    filename = os.fspath(CURRENT_DIRECTORY / "main.qml")
    url = QUrl.fromLocalFile(filename)

    def handle_object_created(obj, obj_url):
        if obj is None and url == obj_url:
            QCoreApplication.exit(-1)

    engine.objectCreated.connect(handle_object_created, Qt.QueuedConnection)
    engine.load(url)

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
import QtQuick 2.14
import QtQuick.Window 2.14

Window {
    id: window
    width: 1920
    height: 1080
    visible: true
    color: "#39393a"
    title: qsTr("PiDeck")


    Text {
        id: dropshadow
        text: qsTr("Searching For Pideck Device...")
        anchors.left: title_text.right
        anchors.top: title_text.bottom
        font.pixelSize: 120
        anchors.leftMargin: -1806
        anchors.topMargin: -131
        font.weight: Font.ExtraBold
        font.family: "Tahoma"
    }

    Text {
        id: title_text
        color: "#ffffff"
        text: qsTr("Searching For Pideck Device...")
        anchors.left: parent.left
        anchors.top: parent.top
        font.pixelSize: 120
        anchors.leftMargin: 53
        anchors.topMargin: 40
        font.weight: Font.ExtraBold
        font.family: "Tahoma"
    }

    TextInput {
        id: ip_input
        width: 830
        height: 121
        color: "#ffffff"
        text: qsTr("Or enter ip manually")
        anchors.left: title_text.right
        anchors.top: title_text.bottom
        font.pixelSize: 80
        anchors.leftMargin: -1813
        anchors.topMargin: 188
        font.weight: Font.ExtraBold
        font.family: "Tahoma"

        Keys.onReturnPressed: {
            backend.foo(ip_input.text)
        }
    }
}

/*##^##
Designer {
    D{i:0;formeditorZoom:0.33}D{i:1}D{i:2}D{i:3}
}
##^##*/