【问题标题】:Call QML function from Python从 Python 调用 QML 函数
【发布时间】:2021-04-09 16:19:17
【问题描述】:

我需要从 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_())

【问题讨论】:

    标签: python qml pyside2


    【解决方案1】:

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

    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
    onClicked: {
        if(bridge.check_tI(textInput.text)){
            dialog1.open()
        }
        else{
            dialog2.open()
        }
    }
    

    【讨论】:

    • 哦,我明白了,@Slot 装饰器中应该有 result=bool 参数。谢谢!如果将来出现类似问题 - 我可以在哪里找到“结果”参数的其他可用值?
    • @dany 默认情况下,除了前面的类型列表之外,它还返回基本类型,如 int、str、bool 和 QObject、QColor。阅读doc.qt.io/qtforpython-5.12/extras/PySide.QtCore.Slot.html
    • 好的,太好了!谢谢!
    猜你喜欢
    • 2013-11-28
    • 2015-09-27
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多