Qt 将属性连接到模块QML文件中的值

Qt 将属性连接到模块QML文件中的值,qt,qml,connection,Qt,Qml,Connection,我有以下调用视图的QML文件(CustomView.QML)。此视图来自名为CustomPages的模块 帮助.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 { } } impor

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

帮助.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
    {

    }
}
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: ""
    }
}
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
    }
}
以下是视图的定义,位于模块
CustomPages

CustomView.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
    {

    }
}
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: ""
    }
}
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
    }
}
此视图调用图形元素以创建页面。这些元素位于另一个名为
CustomGraphics
的模块中。这里有一个:

CustomLabel.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
    {

    }
}
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: ""
    }
}
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
    }
}
正如您可能从所有这些推断的那样,目标是将图形元素与将要更新它们的数据分离

我正在尝试将我的
属性字符串age
(在help.qml中声明)作为
Q_属性(setter、getter、更新图形元素的信号)连接到我的
CustomLabel
的一个值

所有这些都来自
C++
方面

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

编辑:实际上,更新my
CustomLabel
文本
值所需的
Q_属性
已经编码。我不知道的是如何使用相应的
Q\u属性将help.qml文件中声明的属性连接到一个特定的
CustomLabel
(ageLabel)

例如:

// 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)
编辑_2:以下是我目前正在尝试做的事情。 我在我的CustomView.qml中添加了以下行:

Component.onCompleted: cppCode.connectPriorities(age)
然后在
cpp
文件中:

void cpp::connectPriorities(const QVariant &v)
{
    // generic for loop
    // get each variable's name
    // connect it to corresponding CustomLabel
}

为什么不简单地将
help.qml
组件的
age
属性传递给
CustomLabel
组件?因为这样会使视图依赖于数据。如果我想在其他地方使用此视图和另一组数据,该怎么办?关于我在这段时间里提出的问题,请参见EDIT_2。好吧……问题是另一个:如果
help.qml
中的数据发生更改,会发生什么?所有自定义视图都会被破坏,因为它们与另一个组件交互,您在其中进行了一些假设。请提供更多信息,说明您在
CustomLabel.qml