qt quick中如何实现替换掉原来属性的默认值且保持一些内容不变呢?



  • 我的题目可能有点绕哈.

    我有一个property,他的布局是被设定了的。但是如果用户替换掉这个property,怎么才能保持他的布局是和原来的一样呢?

    举例子:

      Bar{
       id: titleBar
       //left controls
    
       property Item leftControls:
           ControlsList{
               parent: titleBar
               anchors.left: parent.left
               anchors.verticalCenter: parent.verticalCenter
           }
    
       //right controls
       property Item rightControls:
           ControlsList{
               parent: titleBar
               anchors.right: parent.right
               anchors.verticalCenter: parent.verticalCenter
           }
      }
    

    我想在leftControls或者rightControls被替换的时候,他的布局总是最初默认的布局。

    怎么实现呢?



  • @Accelerated

    Bar{
        id: titleBar
        //left controls
    
        property Item leftControls: ControlsList{ }
    
        //right controls
        property Item rightControls: ControlsList{ }
        Binding {
           target: leftControls
           property: "parent"
           value: titleBar
        }
        Binding {
           target: leftControls
           property: "anchors.left"
           value: titleBar.left
        }
        Binding {
           target: leftControls
           property: "anchors.verticalCenter"
           value: titleBar.verticalCenter
        }
    
        Binding {
           target: rightControls
           property: "parent"
           value: titleBar
        }
        Binding {
           target: leftControls
           property: "anchors.left"
           value: titleBar.left
        }
        Binding {
           target: leftControls
           property: "anchors.verticalCenter"
           value: titleBar.verticalCenter
        }
    }
    


  • @qyvlikqt quick中如何实现替换掉原来属性的默认值且保持一些内容不变呢? 中说:

    //left controls
    
    property Item leftControls: ControlsList{ }
    
    //right controls
    property Item rightControls: ControlsList{ }
    Binding {
       target: leftControls
       property: "parent"
       value: titleBar
    }
    Binding {
       target: leftControls
       property: "anchors.left"
       value: titleBar.left
    }
    Binding {
       target: leftControls
       property: "anchors.verticalCenter"
       value: titleBar.verticalCenter
    }
    
    Binding {
       target: rightControls
       property: "parent"
       value: titleBar
    }
    Binding {
       target: leftControls
       property: "anchors.left"
       value: titleBar.left
    }
    Binding {
       target: leftControls
       property: "anchors.verticalCenter"
       value: titleBar.verticalCenter
    }
    

    我试试哈~



  • @qyvlik 由于ControlsList是继承Row的,运行了代码后。提示:

      QML Row: Cannot anchor to an item that isn't a parent or sibling.
    

    我需要改变它吗?



  • @Accelerated 要,做一些调整,代码总体框架是没错的。


 

最近的回复

关注我们

微博
QQ群











召唤伊斯特瓦尔