Search code examples
qtqmlqtquick2qtquickcontrols2

Qt Quick Controls 2: Retrieve standard value of a component


I am making a layout in QML and I want to give my Label the same padding as an ItemDelegate.

How can I get the standard padding value of an ItemDelegate?

Thank you in advance!


Solution

  • Firstly, you'll need an instance of an ItemDelegate. If you don't have one, you can create one and set its visible property to false:

    ItemDelegate {
        id: itemDelegate
        visible: false
    }
    

    Some of the built-in styles change as the design guidelines they're based on change, so it's not a good idea to hard-code the padding based on a style's current padding values unless you have control over that style.

    In addition, each style sets a different default padding, and may also use different properties to do so. The following properties can be used to control padding, starting with the most general and ending with the most specific:

    • padding
    • horizontalPadding (available in Qt 5.12)
    • verticalPadding (available in Qt 5.12)
    • leftPadding
    • rightPadding
    • topPadding
    • bottomPadding

    Because of this, the only way to guarantee that you'll get the correct padding for each side of the control is to use the most specific properties:

    Label {
        leftPadding: itemDelegate.leftPadding
        rightPadding: itemDelegate.rightPadding
        topPadding: itemDelegate.topPadding
        bottomPadding: itemDelegate.bottomPadding
    }