Search code examples
qtqmlqtquick2qtquickcontrols2

Styling GroupBox in QML 2


I want to style GroupBox as below in QML 2 :

enter image description here

I am not finding How to do this in QML 2. In the following page, Customizing Qt Quick controls there is no information on Styling Title.


Solution

  • you could do something like this:

    GroupBox {
        id: control
        title: qsTr("GroupBox")
        anchors.centerIn: parent
        width: 300
        height: 150
    
        background: Rectangle {
            y: control.topPadding - control.padding
            width: parent.width
            height: parent.height - control.topPadding + control.padding
            color: "transparent"
            border.color: "#21be2b"
            radius: 2
        }
    
        label: Rectangle {
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.bottom: parent.top
            anchors.bottomMargin: -height/2
            color: "white"                  //set this to the background color
            width: parent.width * 0.7
            height: title.font.pixelSize
            Text {
                id: title
                text: qsTr("My Tilte")
                anchors.centerIn: parent
                font.pixelSize: 32
            }
        }
    }
    

    The more complicated solution if you want your label to be transparent is to use a Canvas to draw the background Rectangle:

    CustomBox.qml

    import QtQuick 2.7
    
    Item {
        id: box
        property string borderColor: "black"
        property int borderWidth: 1
    
    
        onWidthChanged: canvas.requestPaint()
        onHeightChanged: canvas.requestPaint()
    
        Canvas {
            id: canvas
            anchors.fill: parent
            antialiasing: true
    
            onPaint: {
                var ctx = canvas.getContext('2d')
    
                ctx.strokeStyle = box.borderColor
                ctx.lineWidth = box.borderWidth
                ctx.beginPath()
                ctx.moveTo(width*0.15, 0)
                ctx.lineTo(0, 0)
                ctx.lineTo(0, height)
                ctx.lineTo(width, height)
                ctx.lineTo(width, 0)
                ctx.lineTo(width - width * 0.15, 0)
                ctx.stroke()
            }
        }
    }
    

    You can then use it like this:

    GroupBox {
        id: control
        title: qsTr("GroupBox")
        anchors.centerIn: parent
        width: 300
        height: 150
    
        background: CustomBox {
            y: control.topPadding - control.padding
            width: parent.width
            height: parent.height - control.topPadding + control.padding
            borderColor: "black"
            borderWidth: 1
        }
    
        label: Rectangle {
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.bottom: parent.top
            anchors.bottomMargin: -height/2
            color: "transparent"
            width: parent.width * 0.7
            height: title.font.pixelSize
            Text {
                id: title
                text: qsTr("Settings")
                anchors.centerIn: parent
                font.pixelSize: 32
            }
        }
    }