从Python调用QML函数

从Python调用QML函数,python,qml,pyside2,Python,Qml,Pyside2,我需要从QML中获取信息(在本例中是从textInput),对其进行一些操作,并根据操作结果调用QML中的适当函数。我知道如何从textInput中获取文本,但无法根据结果找到如何回复。这是我的密码: main.qml: import QtQuick 2.15 import QtQuick.Window 2.15 import QtQuick.Controls 2.15 ApplicationWindow { width: 640 height: 480 visible

我需要从QML中获取信息(在本例中是从textInput),对其进行一些操作,并根据操作结果调用QML中的适当函数。我知道如何从textInput中获取文本,但无法根据结果找到如何回复。这是我的密码:

main.qml:

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    TextInput {
        id: textInput
        x: 280
        y: 230
        width: 80
        height: 20
        text: qsTr("Text Input")
        font.pixelSize: 12
        horizontalAlignment: Text.AlignHCenter
        selectByMouse: true
    }

    Dialog {
        id: dialog1
        modal: true
        title: "OK"
        Text {text: "Everything is OK!"}
        x: parent.width/2 - width/2
        y: parent.height/2 - height/2
    }

    Dialog {
        id: dialog2
        modal: true
        title: "ERROR"
        Text {text: "Check Internet connection!"}
        x: parent.width/2 - width/2
        y: parent.height/2 - height/2
    }

    Button {
        id: button
        x: 270
        y: 318
        text: qsTr("Check")
        onClicked: {
            bridge.check_tI(textInput.text)
        }
    }
}
main.py:

import sys
import os

from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QObject, Slot, Signal, Property

class Bridge(QObject):

    @Slot(str)
    def check_tI(self, tf_text):
        try:
            # SOME OPERATIONS
            # MUST BE DONE IN PYTHON
            # IF EVERYTHING OK:
            # dialog1.open()
            print("OK! ", tf_text)
        except:
            # dialog2.open()
            print("ERROR! ", tf_text)



if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

    bridge = Bridge()
    engine.rootContext().setContextProperty("bridge", bridge)

    engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))

    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

一种可能的方法是返回一个布尔值,用于决定显示一个或另一个对话框

class Bridge(QObject):
    @Slot(str, result=bool)
    def check_tI(self, tf_text):
        try:
            # trivial demo
            import random

            assert random.randint(0, 10) % 2 == 0
            print("OK! ", tf_text)
        except:
            print("ERROR! ", tf_text)
            return False
        else:
            return True

一种可能的方法是返回一个布尔值,用于决定显示一个或另一个对话框

class Bridge(QObject):
    @Slot(str, result=bool)
    def check_tI(self, tf_text):
        try:
            # trivial demo
            import random

            assert random.randint(0, 10) % 2 == 0
            print("OK! ", tf_text)
        except:
            print("ERROR! ", tf_text)
            return False
        else:
            return True

哦,我明白了,@Slot decorator中应该有result=bool参数。非常感谢。如果将来出现类似问题-在哪里可以找到“result”参数的其他可用值?@dany默认情况下,它会返回基本类型,如int、str、bool和QObject、QColor,以及以前的类型列表。读好,太好了!非常感谢。哦,我明白了,@Slot decorator中应该有result=bool参数。非常感谢。如果将来出现类似问题-在哪里可以找到“result”参数的其他可用值?@dany默认情况下,它会返回基本类型,如int、str、bool和QObject、QColor,以及以前的类型列表。读好,太好了!非常感谢。