首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >QML 2中的GroupBox造型

QML 2中的GroupBox造型
EN

Stack Overflow用户
提问于 2017-10-05 06:41:35
回答 3查看 5.1K关注 0票数 2

我想在QML 2中将GroupBox样式设置为如下:

我在QML 2中找不到如何做到这一点。在下面的页面中,自定义Qt快速控件没有关于样式标题的信息。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-10-05 07:16:28

你可以这样做:

代码语言:javascript
运行
复制
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
        }
    }
}

如果要使标签透明,更复杂的解决方案是使用画布绘制背景矩形:

CustomBox.qml

代码语言:javascript
运行
复制
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()
        }
    }
}

然后,您可以这样使用它:

代码语言:javascript
运行
复制
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
        }
    }
}
票数 5
EN

Stack Overflow用户

发布于 2019-06-16 19:59:06

可以使用label.x将其设置为中间

代码语言:javascript
运行
复制
GroupBox { 
    title: "Potato"
    label.x: width/2 - label.contentWidth/2
    Label {
        text: "Wubba lubba dub dub."
    } 
}
票数 2
EN

Stack Overflow用户

发布于 2019-05-15 14:01:38

试试这个:

代码语言:javascript
运行
复制
GroupBox{
    id: id_keyListBox
    label: Label{
        text: "Choose an account to see details"
        color: "blue"
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46579359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档