如何将值从python传递到qml,反之亦然?

时间:2020-07-24 06:21:53

标签: python pyqt pyqt5 qml

我使用qml和python在Python中进行了以下简单的加法和减法。我可以在Python中获得值,也可以求和和减去。我在互联网上进行搜索,但未找到Pydroid 3的任何工作结果。

所以我的问题是: 如何将结果传递给qml“ sub”和“ sum”文本字段?

还有其他更好的方法吗?

main.py

import sys
from PyQt5.QtCore import QObject, QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView
from PyQt5.QtQml import QQmlApplicationEngine


if __name__ == '__main__':
    myApp = QApplication(sys.argv)

    engine = QQmlApplicationEngine()
    context = engine.rootContext()
    context.setContextProperty("main", engine)

    engine.load('main.qml')

    win = engine.rootObjects()[0]
    
    btnAdd = win.findChild(QObject, "addButton")
    btnSub = win.findChild(QObject, "subButton")
    
    num1 = win.findChild(QObject, "num1")
    num2 = win.findChild(QObject, "num2")
    
    def addFunction():
        sumText = win.findChild(QObject, "sum")
        sum = int(num1.property("text")) + int(num2.property("text"))
        print(sum)
        
    def subFunction ():
        subText = win.findChild(QObject, "sub")
        sub =  int(num1.property("text")) - int(num2.property("text"))
        print(sub)
        
        
    btnAdd.clicked.connect(addFunction)
    btnSub.clicked.connect(subFunction)
    
    win.show()

    sys.exit(myApp.exec_())

main.qml

import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.12
import QtQuick.Controls.Universal 2.12

ApplicationWindow {
    width: 350; height: 560
    
        TextField {
            id: num1
            objectName: "num1"
            placeholderText: qsTr("Number 1")
            focus: true
            anchors.horizontalCenter: parent.horizontalCenter
        }

        TextField {
            id: num2
            anchors.top: num1.bottom
            objectName: "num2"
            placeholderText: qsTr("Number 2")
            focus: true
            anchors.horizontalCenter: parent.horizontalCenter
        }
        
         Button {
            anchors.top: num2.bottom
            signal messageRequired
            objectName: "addButton"
            text: "Add"
            id: addBtn
            onClicked: messageRequired()
            anchors.horizontalCenter: parent.horizontalCenter
        }
        
         Button {
            anchors.top: addBtn.bottom
            signal messageRequired
            objectName: "subButton"
            text: "Sub"
            id: subBtn
            onClicked: messageRequired()
            anchors.horizontalCenter: parent.horizontalCenter
        }
    
        TextField {
           anchors.top: subBtn.bottom
           id: sum
           objectName: "sum"
           placeholderText: qsTr("")
           focus: false
           anchors.horizontalCenter: parent.horizontalCenter
        } 
        
        TextField {
           anchors.top: sum.bottom
           id: sub
           objectName: "sub"
           placeholderText: qsTr("")
           focus: false
           anchors.horizontalCenter: parent.horizontalCenter
        }
  }

0 个答案:

没有答案