2022-06-18

Regarding Qml bindings

Why the below code is not working when clicked on cancel button ie., parameterValueText.text is not setting to value 0.

I am assuming both val and parameterValueText.text are binded eachother. If I am wrong, please correct me

Please find below steps
Step 1: Edit a value in Text Input, for ex: 1
Step 2: on Keyboard accepted, parameterValueText.onAccepted is called
Step 3: Click on Save, saveBtn.onClicked is called
Step 4: Click on Cancel, cancelBtn.onClicked is called but the parameterValueText.text is not changed to value 0.

main.qml

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQuick.VirtualKeyboard 2.1

ApplicationWindow {
id: window
width: 480
height: 272
visible: true
title: qsTr("Keyboard")

property int val: 0

Column {
    Item {
        id: itemId
        height: 20
        width: window.width

        Rectangle{
            width: 100
            height: itemId.height
            border.color:"black"

            TextInput {
                id: parameterValueText
                text: val     //Assuming text and val are  binded ??
                inputMethodHints: Qt.ImhDigitsOnly
                anchors.fill: parent
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                onAccepted:  console.log("Value = ", parameterValueText.text)

            }
        }
    }

    Row {
        spacing: 10
        Button {
            id: saveBtn
            text: "Save"
            onClicked: console.log("Save = ", parameterValueText.text)
        }

        Button {
            id: cancelBtn
            text: "Cancel"
            onClicked: val = 0 //Why the value is not changing in parameterValueText.text ?
        }
    }
}

InputPanel
{
    id:inputPanel
    y:parent.height
    width: window.width

    //Background for Virtual Keyboard
    Component{
        id:keyboardBackground
        Rectangle{
            color:"#f4f6f3"//ScreenConfiguration.backGroundCanvas
        }
    }
    states: State {
        name: "visible"
        when: inputPanel.active
        PropertyChanges {
            target: inputPanel
            y: parent.height - inputPanel.height
        }
        PropertyChanges {
            target: inputPanel
        }
    }
    transitions: Transition {
        from: ""
        to: "visible"
        reversible: true
        ParallelAnimation {
            NumberAnimation {
                properties: "y"
                duration: 200
                easing.type: Easing.InOutQuad
            }
        }
    }
    Component.onCompleted: {
        keyboard.style.keyboardBackground = keyboardBackground;        // the keyboard background
    }
}

}

main.cpp

int main(int argc, char *argv[])
{
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));

QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

QGuiApplication app(argc, argv);

QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                 &app, [url](QObject *obj, const QUrl &objUrl) {
    if (!obj && url == objUrl)
        QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);

return app.exec();
}

Add below change in .pro file for enabling virtual keyboard QT += quick virtualkeyboard

The console log ouputs are below qml: Value = 1, val = 0 qml: Save = 1, val = 0



No comments:

Post a Comment