2016-02-22 58 views
0

我有以下的定制Label访问QML嵌套变量成员从自定义标签

import QtQuick 2.3 
import QtQuick.Controls 1.4 

Label 
{ 
    anchors.centerIn: parent 
    text: "DashboardLabel!" 
    font.pixelSize: 22 
    font.italic: true 
    color: "steelblue" 

    Rectangle 
    { 
     id: rectangle 
    } 
} 

我想通过访问rectangle x和y变量来更改标签的位置:

import QtQuick 2.3 
import QtQuick.Controls 1.4 
import CustomGraphics 1.0 

Item 
{ 
    anchors.centerIn: parent 

    CustomLabel 
    { 
     id: customLabel 
     width: 100 
     height: 100 

     rectangle.x: 200 
    } 
} 

它似乎没有工作,因为我的自定义Label没有移动。我应该使用property功能吗?下面是我得到的错误:

Cannot assign to non-existent property "rectangle" 

编辑:我只是试图以与rect.x访问x添加property alias rect: rectangle。我没有得到任何错误,但没有出现在窗口上。

回答

1

您无法像那样访问子元素的私有属性。您必须创建alias以便子类访问它们。试试这个

import QtQuick 2.3 
import QtQuick.Controls 1.4 

Label 
{ 
    property alias childRect: rectangle 
    anchors.centerIn: parent 
    text: "DashboardLabel!" 
    font.pixelSize: 22 
    font.italic: true 
    color: "steelblue" 

    Rectangle 
    { 
     id: rectangle 
     width: 100 
     height: 100 
    } 
} 

然后

import QtQuick 2.3 
import QtQuick.Controls 1.4 
import CustomGraphics 1.0 

Item 
{ 
    anchors.centerIn: parent 

    CustomLabel 
    { 
     id: customLabel 
     width: 100 
     height: 100 

     childRec.x: 200 
    } 
} 

UPDATE作为OP改变了描述

你还没有为矩形设置widthheight性能。看我的编辑。