[SOLVED] QML TextField placeholderText disappear after push in StackView

Issue

//main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    StackView {
        id: stackView
        anchors.fill: parent
        initialItem: page1
    }

    Item {
        id: page1

        Column {
            height: parent.height * 0.2
            width: parent.width * 0.5
            anchors.centerIn: parent
            spacing: height * 0.04

            TextField {
                height: parent.height * 0.48
                width: parent.width
                placeholderText: qsTr("Placeholder 1")
            }

            Button {
                height: parent.height * 0.48
                width: parent.width
                text: qsTr("Next")
                onClicked: stackView.push(page2)
            }
        }
    }

    Item {
        id: page2

        Column {
            height: parent.height * 0.2
            width: parent.width * 0.5
            anchors.centerIn: parent
            spacing: height * 0.04

            TextField {
                height: parent.height * 0.48
                width: parent.width
                placeholderText: qsTr("Placeholder 2")
            }

            Button {
                height: parent.height * 0.48
                width: parent.width
                text: qsTr("Back")
                onClicked: stackView.pop()
            }
        }
    }
}

Hi everybody, I have a problem with the placehoderText property of TextField.
If i do the sequence -> "Next" on page1 -> "Back" on page2 -> "Next" on page1, then page2 is actually displayed but the placeholderText of the TextField is not visible anymore.
Is this a Qt bug or am I doing something wrong?

Solution

StackView’s goal is not that you tried to use …
you should use SwipeView if you want prevent destroying items.

for your porpuse you can use a little customized SwipeView:

//main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    SwipeView {
        id: swipView
        anchors.fill: parent
        interactive: false;
        property int stackIndex: -1;
        function popIndex(){
            return (--stackIndex);
        }
        function pushIndex(){
            return (++stackIndex);
        }

        Component.onCompleted: {
            if(swipView.contentChildren.length>0)
            {
                swipView.stackIndex=0;
            }
        }

        Item {
            id: page1
            Column {
                height: parent.height * 0.2
                width: parent.width * 0.5
                anchors.centerIn: parent
                spacing: height * 0.04

                TextField {
                    height: parent.height * 0.48
                    width: parent.width
                    placeholderText: qsTr("Placeholder 1")
                }

                Button {
                    height: parent.height * 0.48
                    width: parent.width
                    text: qsTr("Next")
                    onClicked: swipView.currentIndex=swipView.pushIndex();
                }
            }
        }

        Item {
            id: page2

            Column {
                height: parent.height * 0.2
                width: parent.width * 0.5
                anchors.centerIn: parent
                spacing: height * 0.04

                TextField {
                    height: parent.height * 0.48
                    width: parent.width
                    placeholderText: qsTr("Placeholder 2")
                }

                Button {
                    height: parent.height * 0.48
                    width: parent.width
                    text: qsTr("Back")
                    onClicked: swipView.currentIndex=swipView.popIndex();
                }
            }
        }
    }

}

Answered By – H.M

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *