c++qtqmldata-exchange

Passing data from a qml file to other?


I am just starting with Qt Quick and QML.

I wrote a login page which loads a users id after username and password input. After the successful authentication, I need to pass this ID to the new window that is being created.

How can I do that?

login.qml snippet

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;
        }
    }
}

The database stuff is working, I just don't know how to send the userid to the main.qml

Thank you in advance.


Solution

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

    and your main.qml should have the property userid.

    or,

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

    it will make a property userid for win.