stackView.push() does not recognize item



  • I am failing to pass an item to stackView.push(). i have looked at the following and tried all the described options, but all failed:

    http://doc.qt.io/qt-5/qml-qtquick-controls-stackview.html

    Here are my simple files:

    QmlBackbone.qml:

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Dialogs 1.2
    
    ApplicationWindow {
        visible: true
        width: 800
        height: 480
        StackView{
            id: stackView
            width: parent.width
            height: parent.height
            initialItem: MainView {}
        }
    }
    

    MainView.qml:

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Dialogs 1.2
    
    Component {
        id: mainView
    
        Column {
            spacing: 2
            width: parent.width
            height: parent.height
            Rectangle { color: "red"; width: parent.width; height: parent.height/2
                    Label {
                        width: parent.width
                        wrapMode: Label.Wrap
                        horizontalAlignment: Qt.AlignHCenter
                        text: "Testing."
                    }
            }
            Rectangle { color: "green"; width: parent.width; height: parent.height/2
                Label {
                    width: parent.width
                    wrapMode: Label.Wrap
                    horizontalAlignment: Qt.AlignHCenter
                    text: "Settings"
                }
                MouseArea{
                    anchors.fill: parent
                    onClicked: stackView.push(settingView)
                }
            }
        }
    }
    

    SettingView.qml:

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Dialogs 1.2
    
    Component {
        id: settingView
    //    width: parent.width
    //    height: parent.height
    
        Column {
            spacing: 2
            width: parent.width
            height: parent.height
            Rectangle { color: "red"; width: parent.width; height: parent.height/4
                    Label {
                        width: parent.width
                        wrapMode: Label.Wrap
                        horizontalAlignment: Qt.AlignHCenter
                        text: "Height."
                    }
            }
            Rectangle { color: "green"; width: parent.width; height: parent.height/4
                Label {
                    width: parent.width
                    wrapMode: Label.Wrap
                    horizontalAlignment: Qt.AlignHCenter
                    text: "Plot."
                }
            }
            Rectangle { color: "blue"; width: parent.width; height: parent.height/4
                Label {
                    width: parent.width
                    wrapMode: Label.Wrap
                    horizontalAlignment: Qt.AlignHCenter
                    text: "Note."
                }
            }
            Rectangle { color: "orange"; width: parent.width; height: parent.height/4
                Label {
                    width: parent.width
                    wrapMode: Label.Wrap
                    horizontalAlignment: Qt.AlignHCenter
                    text: "Back."
                }
            }
    
        }
    }
    

    The problem is in the onClicked method of the Settings rectangle of the MainView.
    How is that possible and how to solve that problem? All files are in the same folder. I have tried all methods described in the link, I have tried putting the url within QUrl() method, and I have tried methods found elsewhere on the Internet. All of them failed.

    Incidentally, I cannot see any error messages at all neither within QTCreater, which I use to write the qml files, nor in Eclipse (PyDev), which I use to write the PyQt surrounding code. Why does that happen and how to fix that?

    Thank you in advance.



  • Hi @dhb285,

    the problem is, that you do not instantiate your SettingView.

    To solve it, you should make 2 little changes:

    1. Remove the following line from SettingView.qml:
        id: settingView
    
    1. Add an instance of SettingView to your StackView (as you did with MainView):
        StackView{
            ...
            initialItem: MainView {}
            SettingView { id: settingView }
            ...
    

    But I cannot tell why you can't see errors in QtCreator, when I run your code I get the following message in the Application Output:

    qrc:/MainView.qml:29: ReferenceError: settingView is not defined
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.