【问题标题】:Passing data from a qml file to other?将数据从 qml 文件传递​​给其他文件?
【发布时间】:2014-08-26 19:13:45
【问题描述】:

我刚开始使用 Qt Quick 和 QML。

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

我该怎么做?

login.qml sn-p

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

提前谢谢你。

【问题讨论】:

    标签: c++ qt qml data-exchange


    【解决方案1】:
    var win = component.createObject();
    win.userid = login;
    

    你的 main.qml 应该有属性userid

    或者,

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

    它将为win创建一个属性userid

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2020-12-29
      • 2021-08-15
      • 1970-01-01
      • 2021-11-15
      • 2017-05-08
      • 2021-02-10
      相关资源
      最近更新 更多