Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Settings.value() use case



  • Good afternoon,
    I am trying to learn more of QML and as a new year exercise I am attempting to convert (rewrite) one of my existing programs into QML frontend. It is going quite well but I think I am doing something very silly here.
    I have a functionality where user can choose for program to remember and restore last login in to program. In QtWidgets instance this was resolved using QCheckBox, QInputLine and, obviously, QSettings. While I had no problem in rewriting the UI part, restoring simple string seems to be a problem.
    The code:

    Drawer {
        id: drawer
    
        Settings {
           property alias restoreLogin: saveUser.checked
        }
    
        GroupBox {
            id: loginBox
            title: qsTr("Login credentials")
            
            ColumnLayout {
                RowLayout {
                    Label {
                        text: qsTr("User name:")
                    }
                    TextInput {
                        id: uname
                        text: saveUser.checked ? Settings.value("lastLogin","") : ""
                    }
                    CheckBox {
                        id: saveUser
                        text: qsTr("Remember login")
                    }
                }
                RowLayout {
                    Label {
                        text: qsTr("Password:")
                    }
                    TextInput {
                        id: upass
                        echoMode: TextInput.Password
                    }
                }
                Button {
                    text: qsTr("OK")
                }
            }       
        }
    }
    

    The line: text: saveUser.checked ? Settings.value("lastLogin","") : ""
    gives me the error: TypeError: Property 'value' of object [object Object] is not a function

    What do I miss?



  • @artwaw

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Layouts 1.12
    import QtQuick.Controls 2.12
    import Qt.labs.settings 1.0
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        Settings {
            id:st
            property alias restoreLogin: saveUser.checked
            property alias usrName :  uname.text
        }
    
        Component.onCompleted:{
    
            if(st.restoreLogin){
                uname.text = st.usrName
            }
            else{
                uname.text = ""
            }
        }
    
        GroupBox {
            id: loginBox
            title: qsTr("Login credentials")
    
            ColumnLayout {
                RowLayout {
                    Label {
                        text: qsTr("User name:")
                    }
                    TextField {
                        id: uname
                    }
    
                    CheckBox {
                        id: saveUser
                        checked: st.restoreLogin
                        text: qsTr("Remember login")
                    }
                }
                RowLayout {
                    Label {
                        text: qsTr("Password:")
                    }
                    TextInput {
                        id: upass
                        echoMode: TextInput.Password
                    }
                }
                Button {
                    text: qsTr("OK")
                }
            }
        }
    }
    
    


  • @artwaw

    The line: text: saveUser.checked ? Settings.value("lastLogin","") : ""

    Property values are always starting with lower case letters ,may be for that reason only that is showing error. make changes in Settings.value to

    saveUser.checked ? settings.value("lastLogin","") : ""

    hope it will works.



  • Hi @artwaw, what you are doing on Settings.value is not permitted by QML. You need to set an object id to your Settings object and then access that object via id.
    i.e:
    Settings {
    id: "settingsObject"
    property alias restoreLogin: saveUser.checked
    }

    And then use settingsObject.value("lastLogin","")



  • @artwaw

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Layouts 1.12
    import QtQuick.Controls 2.12
    import Qt.labs.settings 1.0
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        Settings {
            id:st
            property alias restoreLogin: saveUser.checked
            property alias usrName :  uname.text
        }
    
        Component.onCompleted:{
    
            if(st.restoreLogin){
                uname.text = st.usrName
            }
            else{
                uname.text = ""
            }
        }
    
        GroupBox {
            id: loginBox
            title: qsTr("Login credentials")
    
            ColumnLayout {
                RowLayout {
                    Label {
                        text: qsTr("User name:")
                    }
                    TextField {
                        id: uname
                    }
    
                    CheckBox {
                        id: saveUser
                        checked: st.restoreLogin
                        text: qsTr("Remember login")
                    }
                }
                RowLayout {
                    Label {
                        text: qsTr("Password:")
                    }
                    TextInput {
                        id: upass
                        echoMode: TextInput.Password
                    }
                }
                Button {
                    text: qsTr("OK")
                }
            }
        }
    }
    
    


  • Hi @artwaw ,

    you have to use Object id name to call the function of Certain Object.

    Settings {
    id: settings
    property alias restoreLogin: saveUser.checked
    }
    The line: text: saveUser.checked ? settings.value("lastLogin","") : ""



  • Thank you all for your input, I've set id property and used that, works now.
    I knew it was something silly :)


Log in to reply