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

Changing enabled properties depending of user rights



  • Hello

    I'm trying to disable options for users in a list depending of their rights. Here is the idea :

    main.qml

    [ListModel {
            id: modelActivityDataId
            ListElement {
                title: qsTr("Login")
                image: "qrc:/res/site_load.png"
                page: "Login.qml"
            }
            ListElement {
                title: qsTr("Edition de Site")
                image: "qrc:/res/configuration.png"
                page: "SiteForm.qml"
            }
            ListElement {
                title: qsTr("Param├Ętres")
                image: "qrc:/res/applications-system.png"
                page: "ConfigurationForm.qml"
            }
    }
    StackView {
            id: stackView
            anchors.fill: parent
            focus: true
            Keys.onReleased: if (event.key === Qt.Key_Back && stackView.depth > 1) {
                                 stackView.pop();
                                 event.accepted = true;
                             }
    
            initialItem: Item {
                width: parent.width
                height: parent.height
                ListView {
                    model: modelActivityDataId
                    anchors.fill: parent
                    delegate: ThemeDelegate {
                        id: modelActivityDelegateId
                        text: title
                        imagefile : image
                        onClicked: stackView.pushItem(Qt.resolvedUrl(page))
                        states:
                        [
                            State
                            {
                                name : "defaultState"
                                PropertyChanges
                                {
                                    target: modelActivityDataId
                                    enabled: index === 0
                                }
                            },
                            State
                            {
                                name : "userLogged"
                                PropertyChanges
                                {
                                    target: modelActivityDataId
                                    enabled: index === 0 || index === 1
                                }
                            },
                            State
                            {
                                name : "adminLogged"
                                PropertyChanges
                                {
                                    target: modelActivityDataId
                                    enabled: index === 0 || index === 1 || index === 2
                                }
                            }
                        ]
                    }
                    Connections
                    {
                        target: userRights
                        onSglIsOut: modelActivityDelegateId.state = "defaultState"
                    }
                    Connections
                    {
                        target: userRights
                        onSglIsUser: modelActivityDelegateId.state = "userLogged"
                    }
                    Connections
                    {
                        target: userRights
                        onSglIsAdmin: modelActivityDelegateId.state = "adminLogged"
                    }
                }
            }
    ]
    

    the signals are sent from a Cpp classe userRightManager
    However, the states aren't recognised by the delegate...can't find a way to get them right...


  • Moderators

    Hi @the-moebius,
    AFAIK delegates can't be accessed directly from outside . You will need to iterate the ListView children and change the state.
    But the best way would be to do it through model.


Log in to reply