簡體   English   中英

如何將數據從 main.qml 傳遞到 main.py,然后將其發送到其他 qml 文件

[英]How do you pass data from main.qml to main.py then send it to other qml files

我想將文本字段中的數據發送到后端,即 main.py 文件。 然后,function 將通過在文本字段中添加輸入內容來連接字符串“Welcome”。 然后,此字符串將顯示在 label 中,該 label 通過主頁上的堆棧推送在第三個文件中找到。 連接后端和前端后,我的程序只顯示 Welcome 沒有文本字段輸入

main.qml

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

Window {
    width: 640
    height: 480
    visible: true
    color: "#00000000"
    title: qsTr("Hello World")

    Rectangle {
        id: rectangle
        color: "#ffffff"
        anchors.fill: parent

        StackView {
            id: stackView
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.bottom: button.top
            anchors.rightMargin: 10
            anchors.leftMargin: 10
            anchors.bottomMargin: 5
            anchors.topMargin: 5
        }

        Button {
            id: button
            x: 368
            y: 396
            text: qsTr("Button")
            anchors.right: parent.right
            anchors.bottom: parent.bottom
            anchors.rightMargin: 10
            anchors.bottomMargin: 5
            onClicked: stackView.push("home.qml")
            onPressed: {
                backend.welcomeText(txtName.text)
            }
        }

        TextField {
            id: txtName
            x: 92
            y: 436
            placeholderText: qsTr("Text Field")
        }
    }

    Connections {
        target: backend

        function onGetName(name){
            welcomeLabel.text = name
        }
    }
}

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

主頁.qml

import QtQuick 2.0
import QtQuick.Controls 2.15

Item {
    Rectangle {
        id: rectangle
        color: "#262626"
        anchors.fill: parent

        Label {
            id: welcomeLabel
            x: 251
            y: 204
            width: 251
            height: 82
            color: "#e9eaeb"
            text: qsTr("Welcome")
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
    }

}

主文件

import sys
import os

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

class MainWindow(QObject):
    def __init__(self):
        QObject.__init__(self)

    #getName
    getName = Signal(str)

    @Slot(str)
    def welcomeText(self, name):
        self.getName.emit("Welcome " + name)


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

    main = MainWindow()
    engine.rootContext().setContextProperty("backend", main)
    engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))

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

主要問題是welcomeLabel只有一個“home.qml”的scope,所以它沒有在main.qml中定義。

由於“后端”是一個 contextProperty,它具有全局 scope(類似於全局變量),因此您必須在 home.qml 中連接“getName”信號。 但問題是您在頁面加載之前發出信號,因此連接將無法正常工作,在這種情況下,解決方案是先加載然后調用插槽。

您實際案例中的解決方案:

  1. 將“連接”代碼從 main.qml 移動到 home.qml

  2. 並更改(刪除pressed ):

onClicked: {
    stackView.push("home.qml")
    backend.welcomeText(txtName.text)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM