从自定义Label访问QML嵌套变量成员(Access QML nested variable member from custom Label)

编程入门 行业动态 更新时间:2024-10-26 20:23:53
从自定义Label访问QML嵌套变量成员(Access QML nested variable member from custom Label)

我有以下自定义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.

更多推荐

本文发布于:2023-08-04 08:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412752.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   自定义   变量   成员   QML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!