我有以下自定义Label :
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"编辑 :我刚刚尝试添加property alias rect: rectangle以便使用rect.x访问x 。 我没有收到任何错误,但窗口上没有任何内容。
I have the following custom Label:
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 } }I'm trying to change the position of the label by accessing the x and y variables from rectangle:
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 } }It doesn't seem to be working since my custom Label is not moved. Should I use the property feature? Here is the error I'm getting:
Cannot assign to non-existent property "rectangle"EDIT: I've just tried to add property alias rect: rectangle in order to access x with rect.x. I do not get any errors but nothing appears on the window.
最满意答案
您无法像这样访问子元素的私有属性。 您必须创建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 } }更新后 ,OP改变了描述
您尚未设置矩形的width和height属性。 看我的编辑。
You can't access the private properties of the child element like that. You have to create alias in order for the subclass to access them. Try this
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 } }and then
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 as OP changed the description
You haven't set width and height properties for the rectangle. See my edit.
更多推荐
发布评论