首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QML AppSwitch每次打开页面发送onCheckedChanged信号?

QML AppSwitch每次打开页面发送onCheckedChanged信号?
EN

Stack Overflow用户
提问于 2019-06-29 12:17:22
回答 1查看 356关注 0票数 0

我有一个AppSwitch,它在我的应用程序中被用来“激活/停用”用户帐户,它的检查状态是从一个数据库中读取的,该数据库按预期工作,根据每个用户的bool改变开关和文本。

我的问题是

每次我打开带有AppSwitch的页面时,它都会发送onCheckedChanged信号--然后向用户发送通知--他们的帐户已经激活了?我曾计划通过推送通知通知用户他们的帐户是活动的,但不希望每次管理员移动到他们的页面时都这样发送!

我使用Felgo(以前的V-Play)进行开发,AppSwitch文档的链接是:

费尔戈AppSwitch

我的代码是:

代码语言:javascript
复制
            Rectangle {               
                width: parent.width / 2
                height: parent.height
                Column {
                    id: column
                    anchors.fill: parent
                    AppButton {
                        id: groupStatusText
                        width: parent.width
                        height: parent.height / 2
                    }
                    AppSwitch {
                        id: editStatus
                        knobColorOn: "#b0ced9"
                        backgroundColorOn: "#81b1c2"
                        checked: {
//userListItem is the current profile details of the user page
                            if(userListItem.groupStatus === 1) {
                                groupStatusText.text = "Deactivate this user"
                            }
                            else if(userListItem.groupStatus === 0) {
                                groupStatusText.text = "Activate this user"
                            }
                        }
                        onCheckedChanged: {
                            if(editStatus.checked === true) {
//the signal which sends to my database on updating the value,
//works as intended but sends signal each time page is created?
                                detailPage.adminEditGroupStatus(1)
                            } else {
                                detailPage.adminEditGroupStatus(0)
                            }
                        }
                    }
                }

在加载页面的每个实例上触发onCheckedChanged信号是正常行为吗?或者,我如何修改它,使它只在用户更改它时才会触发!?

谢谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-01 17:12:33

添加一个条件,以确保在组件完整插入后(包括在checked属性上创建绑定之后)发送信号。

您可以在开关上添加一个布尔属性:

代码语言:javascript
复制
AppSwitch {
    property bool loaded: false    

    Component.onCompleted: loaded = true

    onCheckedChanged: {
        if (loaded) {
            // notify user
        }
    }
}

或者简单地使用state

代码语言:javascript
复制
AppSwitch {    
    Component.onCompleted: state = "loaded"

    onCheckedChanged: {
        if (state === "loaded") {
            // notify user
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56817460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档