2

In QML StackView docs it is mentioned that you can push item with properties like this:
stackView.push({item: someItem, properties: {fgcolor: "red", bgcolor: "blue"}})

Is there a way with which we can push component with properties? My components are basically wrappers of other .qml files for different views of my app, for example:

Component{
    id: loginComponent
    Login{}//The Login.qml file of my project
}

This is what I'm trying:
Main.qml

    ApplicationWindow {
    id: appWindow
    visible: true
    width: Screen.desktopAvailableWidth
    height: Screen.desktopAvailableHeight
    property alias stackv: stackv
    property alias loginComponent: loginCom
        StackView {
        id: stackv
        anchors.top: topHeader.bottom
        anchors.topMargin: 10
        anchors.bottom: parent.bottom
        width: parent.width
        focus: true
            Component {
            id: loginCom
            Login {
                anchors.fill: parent
            }
        }
    }
    }

In another QML file, which got pushed as a component to the stackview, I'm trying this on one of the button's onClick method:

onClicked: {
            appWindow.stackv.push({item: appWindow.loginComponent})
        }

I get popped with this error:

QML StackView: push: nothing to push

If I try this without the item property, it works. However, either way, I can't push properties.

Akash Agarwal
  • 2,326
  • 1
  • 27
  • 57

1 Answers1

6

In the documentation you linked to, the first sentence says:

An item pushed onto the StackView can be either an Item, a URL, a string containing a URL, or a Component.

So just pass a component:

stackView.push({item: loginComponent, properties: {/*...*/}})

EDIT: It turns out, after you have edited the question and added a warning output, that you are actually using StackView from Qt Quick Controls 2, not from Qt Quick Controls 1 where your documentation link points to.

jpnurmi
  • 5,716
  • 2
  • 21
  • 37
  • 1
    Make sure to read the documentation of the correct control set, because there are some minor API differences in those two different major versions. Furthermore, for the future reference, when you paste a QML file, please do not leave out the imports. – jpnurmi Jul 05 '16 at 07:22
  • Embarrassing, thanks for pointing that out. I'll take care in future. – Akash Agarwal Jul 06 '16 at 16:15