[Solved]Element Template



  • 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"
    }
    }@


  • Moderators

    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.



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

    }
    @



  • 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.



  • 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.



  • How to implement it?


  • Moderators

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



  • Yeah currently reading, thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.