Sanket Joshi
Sanket Joshi

Reputation: 158

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

This is my Box.qml
I want to use this Box as customized QML Component.

 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
           }
      }

This is my main.qml

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

But this is not working I am getting Following Error

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

Upvotes: 1

Views: 2261

Answers (1)

eyllanesc
eyllanesc

Reputation: 244132

You have to expose that property through 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"
   }
}

Or use 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"
   }
}

Upvotes: 3

Related Questions