In a QML application I'm coding, I am always writing the sames attributes names with different values. For example :
Image{
source:"qrc:/..."
Layout.preferredWidth: 124 //Always
Layout.preferredHeight: 229 //the same
Layout.alignment: Qt.AlignHCenter //3 lines
Layout.topMargin: convertHeight(40)
Layout.bottomMargin: convertHeight(26)
}
would it be possible to define a function that generates dynamic attributes :
function sizeProps(width,height){
return {
Layout.preferredWidth : width,
Layout.preferredHeight : height,
Layout.alignment: Qt.AlignHCenter
}
}
and use the dynamically generated attributes like this ?
Image{
source:"qrc:/..."
{sizeProps(124,229)}
Layout.topMargin: convertHeight(40)
Layout.bottomMargin: convertHeight(26)
}
You can't do this, but you can create a custom component and reuse your new type. [1]
Either by defining an inline component like:
Or defining it in a separate
.qml
file [3].Img.qml
But if you prefer using functions, you can do this too, but I do not recommend it.