将数据从qml文件传递给其他文件

Passing data from a qml file to other?

本文关键字:文件 其他 qml 数据      更新时间:2023-10-16

我刚从Qt Quick和QML开始。

我写了一个登录页面,在输入用户名和密码后加载用户id。成功身份验证后,我需要将此ID传递给正在创建的新窗口。

我该怎么做?

login.qml摘录

BSButton {
        id: btnOK
        anchors.top:senhaInput.bottom
        anchors.left: senhaInput.left
        anchors.topMargin: 10
        width: (senhaInput.width * 0.60) - 5
        text: "Entrar"
        isDefault: true
        onClicked: {
            lblMsgErro.text = ""
            lblMsgErro.visible = false;
            controller.autenticar(); // returns user id to pass to main.qml
        }
    }
QLoginController {
    id: controller
    login: loginInput.text
    senha: senhaInput.text
    onAuthenticated: {
        if (success) {
            var component = Qt.createComponent("main.qml");
            var win = component.createObject();
            win.showFullScreen();
            close();
        } else {
            senhaInput.text = "";
            console.log("Falha na autenticação: Usuário e/ou senha inválidos.");
            lblMsgErro.text = "Usuário e/ou senha inválidos.";
            lblMsgErro.visible = true;
            loginInput.focus = true;
        }
    }
}

数据库的东西正在工作,我只是不知道如何将用户ID发送到main.qml

提前谢谢。

var win = component.createObject();
win.userid = login;

并且您的main.qml应该具有属性userid

或者,

var win = component.createObject(controller, {'userid':login});

它将为CCD_ 3生成属性CCD_。