Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved]Element Template
Forum Updated to NodeBB v4.3 + New Features

[Solved]Element Template

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 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.
  • O Offline
    O Offline
    onek24
    wrote on last edited by
    #1

    Hey,

    is it possible to create a Template of a QML Element or an own QML Element without using Cpp? I've got a lot of Labels and Textelements which are using a same format and it would be better to set the format once for all Labels, is that possible?

    Example:
    @Item {
    Label {
    font.size: 14
    text: "Some Text"
    }
    Label {
    font.size: 14
    text: "More Text"
    }
    }@

    I would like to have it like that:
    @Item {
    MyLabel { // Already font size 14
    text: "Some Text"
    }
    MyLabel { // Already font size 14
    text: "More Text"
    }
    }@

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Sure! All you need to do is to put your label into a new QML file and include it:
      @
      // MyLabel.qml
      Label {
      font.size: 14
      }
      @

      You need to make sure the file name (MyLabel.qml) begins with a capital letter, otherwise the QML engine will not pick it up. You include the file using standard import statement.

      (Z(:^

      1 Reply Last reply
      0
      • U Offline
        U Offline
        unai_i
        wrote on last edited by
        #3

        Hi,
        You just have to create a component for this.
        Either in a .qml file:
        MyLabel.qml
        @
        Label {
        font.size: 14
        }
        @

        or declaring a component in your QML code:
        @
        Component{
        id: myLabel

        Label {
            font.size: 14
            text: "More Text"
        }
        

        }
        @

        1 Reply Last reply
        0
        • U Offline
          U Offline
          unai_i
          wrote on last edited by
          #4

          Well I see sierdzio has been faster than me ;)
          Also for the second solution I proposed you won't be able to declare your elements as in your code above but through dynamic object creation, Repaeter, Loader or similar elements.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            onek24
            wrote on last edited by
            #5

            Thank you guys very much. I already had tried this but i didn't know about the capital letter. At least it is working now.

            1 Reply Last reply
            0
            • O Offline
              O Offline
              onek24
              wrote on last edited by
              #6

              How to implement it?

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                For that solution, you need a Loader or an Repeater. See the documentation.

                (Z(:^

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  onek24
                  wrote on last edited by
                  #8

                  Yeah currently reading, thank you.

                  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