簡體   English   中英

如何在其他qml文件中設置'Text'QML Component的文本屬性?

[英]how can i set text property of 'Text' QML Component in other qml file?

這是我的Box.qml
我想將此Box用作自定義QML組件。

 import QtQuick 2.5
      Rectangle{
            id: sample
            width: 100
            height: 35
            border.color: "Black"
            color: "#778899"
            Text{
                font.pointSize: 10
                font.bold: true
                anchors.centerIn: parent
           }
      }

這是我的main.qml

 import QtQuick 2.5
 import QtQuick.Window 2.2
 Window {
   visible: true
   width: 640
   height: 480
   title: qsTr("Repeater")
   Box{
       text: "Number"
   }
}

但這不起作用我得到以下錯誤

qrc:/main.qml:11 Cannot assign to non-existent property "text"

您必須通過property公開該property

Box.qml

import QtQuick 2.5
Rectangle{
    property string mytext
    id: sample
    width: 100
    height: 35
    border.color: "Black"
    color: "#778899"
    Text {
        font.pointSize: 10
        font.bold: true
        anchors.centerIn: parent
        text: mytext
    }
}

main.qml

import QtQuick 2.5
import QtQuick.Window 2.2
Window {
   visible: true
   width: 640
   height: 480
   title: qsTr("Repeater")
   Box{
       mytext: "Number"
   }
}

或使用alias

Box.qml

import QtQuick 2.5
Rectangle{
    property alias text: txt.text
    id: sample
    width: 100
    height: 35
    border.color: "Black"
    color: "#778899"
    Text {
        id: txt
        font.pointSize: 10
        font.bold: true
        anchors.centerIn: parent
    }
}

main.qml

import QtQuick 2.5
import QtQuick.Window 2.2
Window {
   visible: true
   width: 640
   height: 480
   title: qsTr("Repeater")
   Box{
       text: "Number"
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM