2016-02-22 57 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性能。看我的編輯。