How to apply my own custom styling to text elements?



  • I have the following code:

    Text {
        id: tid
        text: qsTr("Tid")
    
        width: parent.width           //this is the part i want to style on every headline element
        verticalAlignment: Text.AlignVCenter
        horizontalAlignment: Text.AlignHCenter
        Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
        font.pixelSize: 24
    }
    

    I want to repeat all of the code and use on other element like

    Text {
          id: tid2
          text: qsTr("tid2")
          
           (some kind of headline styling)
    }
    

    How do i make a some kind of style, that i can use repeatly on every headline element?


  • Moderators

    @JesperBorri Custom reusable components are quite easy to make. Just add your stylized Text code inside a separate QML file say for eg. MyText.qml. So now whenever you want to use this stylied custom component is another qml code, just import this file and load MyText component inside it. All your information is contained inside this loaded component. That's it.


Log in to reply
 

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