将属性连接到Module QML文件

时间:2016-02-22 13:31:39

标签: qt qml connection

我有以下QML文件调用视图( CustomView.qml )。此视图来自名为CustomPages的模块。

help.qml

import QtQuick 2.5
import QtQuick.Controls 1.4
import CustomPages 1.0
import CustomGraphics 1.0

Item
{
    property string age
    anchors.fill: parent

    CustomView
    {

    }
}

以下是视图的定义,位于模块CustomPages中:

CustomView.qml

import QtQuick 2.3
import QtQuick.Controls 1.4
import CustomGraphics 1.0

Item
{
    anchors.centerIn: parent

    CustomLabel
    {
        id: ageLabel
        childLabel.text: ""
    }

    CustomLabel
    {
        id: weightLabel
        childLabel.text: ""
    }
}

此视图调用图形元素以创建页面。这些元素位于另一个名为CustomGraphics的模块中。这是一个:

CustomLabel.qml

import QtQuick 2.3
import QtQuick.Controls 1.4

Item
{
    property alias childLabel: label

    Label
    {
        id: label
        text: "Custom Label"
        color: "green"
        x: 10
        y: 50
        // many other attributes which customize the Label
    }
}

正如您可能从所有这些中推断出来的,目标是将图形元素与将要更新它们的数据分开。

我正在尝试连接我的property string age(在 help.qml 中声明)作为Q_PROPERTY(setter,getter,更新的信号)图形元素)到我CustomLabel的一个值。

所有这些来自C++方。

为什么呢?它允许我轻松更新每个图形元素的值(样式,位置等),而无需修改视图页面本身。数据更新机制也将与它们分开。

编辑:事实上,更新我Q_PROPERTY的{​​{1}}值所需的text已经过编码。我不知道的是如何使用相应的{{将 help.qml 文件中声明的属性连接到一个特定的CustomLabel ageLabel ) 1}}。

示例:

CustomLabel

EDIT_2 :这就是我目前正在尝试做的事情。 我在 CustomView.qml 中添加了以下行:

Q_PROPERTY

然后在// help.qml property string age // C++ model Q_PROPERTY(int age READ age WRITE setAge NOTIFY ageChanged) // C++ side // connect property string age to the specific CustomLabel designed for the age value. // in CustomView.qml // updates the corresponding CustomLabel (id: ageLabel) 档案中:

Component.onCompleted: cppCode.connectPriorities(age)

0 个答案:

没有答案