【问题标题】:Component pushed on StackView does not bind correctlyStackView 上推送的组件未正确绑定
【发布时间】:2016-09-21 15:09:23
【问题描述】:

下面的例子说明了我的问题。 我在左上角创建了一个小的Rectangle,点击它可以在红色和绿色之间切换颜色。

接下来,我创建一个 StackView 并将 Rectangle 推送到 StackView 并将第二个 Rectangle 的颜色绑定到左上角矩形的颜色

预期的行为是,单击左上角的Rectangle 也会更改StackViewRectangle 的颜色,因为颜色已绑定。不幸的是,事实并非如此。

请注意,将 stackRect2 推送到堆栈时一切正常(请参阅注释中的行)

import QtQuick 2.0
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

Window {
    id: mainWindow
    visible: true
    width: 1280
    height: 720

    Rectangle {
        id: rect
        width: 100
        height: 100
        focus: true

        color: toggle? "red":"green"
        property var toggle:false;
        MouseArea {
            anchors.fill: parent
            onClicked: rect.toggle = !rect.toggle
        }
    }

    StackView {
        id: stack
        width: 100
        height:100
        anchors.left: rect.right
        anchors.leftMargin: 10

        Component.onCompleted: {
            stack.push ({item:stackRect, properties: {color:rect.color}})
            //stack.push ({item:stackRect2})
        }
    }

    Component {
        id:stackRect
        Rectangle {}
    }
    Component {
        id:stackRect2
        Rectangle {color:rect.color}
    }
}

【问题讨论】:

  • 不能肯定,但我猜 StackView 的内部工作会导致绑定中断。可能与此有关:具有绑定的属性会根据需要自动更新。但是,如果稍后从 JavaScript 语句中为该属性分配了一个静态值,则该绑定将被删除。

标签: qt qml qtquick2 qtquickcontrols stackview


【解决方案1】:

Apparently,此行为是预期行为,符合Component::createObject()。 使用

    stack.push (stackRect,  {color:Qt.binding(function() { return rect.color})})

工作得很好。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-06-19
  • 2018-02-12
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-25
  • 1970-01-01
相关资源
最近更新 更多