Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Chinese
  4. 请问怎样的同一个qml 文件中实现自定义的组件重用?
Forum Updated to NodeBB v4.3 + New Features

请问怎样的同一个qml 文件中实现自定义的组件重用?

Scheduled Pinned Locked Moved Solved Chinese
3 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • neemeN Offline
    neemeN Offline
    neeme
    wrote on last edited by
    #1

    大家好:
    如下 tmp_01 要实现10个, 如果重复10代码感觉很难看,又不想独立一个文件放tmp_01 。 10个tmp_01,都一样, 但要能改变 text ( property alias text: t1.text).
    请问怎样实现? 谢谢!

    Item {
    id: tmp_01
    width: parent.width
    height: 100
    property alias text: t1.text

        Text {
            id: t1
            text: qsTr("line-1")
            font.pixelSize: 20
            anchors.centerIn: parent
            color: "yellow"
        }        
    }
    
    joeQJ 1 Reply Last reply
    0
    • neemeN neeme

      大家好:
      如下 tmp_01 要实现10个, 如果重复10代码感觉很难看,又不想独立一个文件放tmp_01 。 10个tmp_01,都一样, 但要能改变 text ( property alias text: t1.text).
      请问怎样实现? 谢谢!

      Item {
      id: tmp_01
      width: parent.width
      height: 100
      property alias text: t1.text

          Text {
              id: t1
              text: qsTr("line-1")
              font.pixelSize: 20
              anchors.centerIn: parent
              color: "yellow"
          }        
      }
      
      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #2

      @neeme 你好. 尝试一下,下面组件加载的方法

       import QtQuick 2.0
      
        Item {
            width: 100; height: 100
      
            Component {
                id: redSquare
      
                Rectangle {
                    color: "red"
                    width: 10
                    height: 10
                }
            }
      
            Loader { sourceComponent: redSquare }
            Loader { sourceComponent: redSquare; x: 20 }
        }
      

      Just do it!

      1 Reply Last reply
      0
      • neemeN Offline
        neemeN Offline
        neeme
        wrote on last edited by
        #3

        但我怎样改变 t1.text 的值呢? 因为每个的值都不同

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved