首页 文章

QML双向C绑定不再接收更改

提问于
浏览
3

我面临一个很大的问题,它需要花费很多时间来修复,因为我不知道原因以及如何解决它 . 问题非常简单:我有一个示例QML组件定义为:

Rectangle {
    id: rect
    property bool test: myclass.testproperty

    Rectangle {
        width: 50
        height: 50
        visible: parent.test
    }
}

我连接了一个MouseArea onClicked信号来执行此操作:

test = !test

所以我切换布尔变量的值 . 要使用READ,WRITE和NOTIFY信号将值从C推送到QML,从QML推送到C Q_PROPERTY,我使用了这个

Binding {
    target: myclass
    property: "testproperty"
    value: rect.test
}

一切正常,直到我点击mouseArea,所以我通过绑定推送更改 . 之后,每次我尝试从C设置新的属性值时,我都没有看到QML发生任何变化,就像绑定被破坏一样 . 但是如果我尝试单击MouseArea,我仍然会调用C类的setTestProperty方法 . 基本上,它与C - > QML方式不同步 . 为什么?我找不到问题,发出信号是因为QSignalSpy在使用后给出了1作为发射次数的计数

emit this->testPropertyChanged(newvalue)

EDIT

这里有一个例子:基本上我们在这里使用具有相同精确信号的QString属性 . 唯一改变的是,我使用TextInput元素而不是使用Rectangle QML元素并绑定到自己的属性 .

TextInput {
    id: mytext
    text: myclass.testproperty
}

Binding {
    target: myclass
    property: "testproperty"
    value: mytext.text
}

1 回答

  • 5

    这里没有问题 . 它是标准的QML绑定行为 . 当您在JavaScript代码中更改某些QML Widget的属性时,将终止对它的所有声明性绑定 . 您可以选择在事件处理程序的JS代码中手动使用声明性绑定或更新值 .

    EDIT

    Rectangle {
        id: rect
    
        // Establishing initial value for 'test' property via QML binding
        property bool test: myclass.testproperty
    
        // Manual update of 'test' property when binding will be broken
        Connections {
            target: myclass
            onTestpropertyChanged: {
                rect.test = xxx
            }
        }
    }
    

相关问题