QT QML 界面设计教程1——按钮样式

MyButton.qml

import QtQuick 2.14
import QtQuick.Templates 2.14 as Template1
import QtQuick.Controls 2.14
import QtQuick.Controls.impl 2.14

Template1.Button {
    id:control

    
    //checked选中状态,down按下状态,hovered悬停状态
    property color textColor: "white"
    property color backgroundTheme: "#36c588"
    property color backgroundColor: control.down
                                    ? Qt.darker(backgroundTheme)
                                    : (control.hovered||control.highlighted)
                                      ? Qt.lighter(backgroundTheme)
                                      : control.checked
                                        ? backgroundTheme
                                        : backgroundTheme
    property int radius: 0     //背景rect的圆角

    
    implicitWidth: 90
    implicitHeight: 30
    leftPadding: 0
    rightPadding: 5
    spacing:0
    //checkable: true
    font{
        family: "SimSun"
        pixelSize: 30
    }

    //icon.width: 24
    //icon.height: 24

    //按钮中显示的内容
   
    contentItem: Row{
        id:btn_row
        width: control.width
        spacing: control.spacing
        Image {
            id:btn_icon
            anchors{
                verticalCenter: parent.verticalCenter
            }
            source: control.icon.source //借用icon的接口
        }
        Text{
            id:btn_text
            anchors{
                verticalCenter: parent.verticalCenter
            }
            width: btn_icon.width
                   ? btn_row.width-btn_row.spacing-btn_icon.width
                   : btn_row.width
            text: control.text
            color: control.textColor
            renderType: Text.NativeRendering
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            //wrapMode: Text.NoWrap
            elide: Text.ElideRight
            font: control.font
        }
    }

    //背景
    background: Rectangle{
        implicitWidth: control.implicitWidth
        implicitHeight: control.implicitHeight
        radius: control.radius
        //visible: !control.flat || control.down || control.checked || control.highlighted
        color: control.backgroundColor
    }
}

调用:

        MyButton {
            id: logoButton
            anchors.left: parent
            x: -12
            y: -11
            width: 200
            height: 98
            text: qsTr("Mike-sh")
            font.pointSize: 70
            font.family: "Times New Roman"
        /*
        background: Rectangle {
                color: "#1d8836"
                radius: 0
                border.color: "#1d8836"
                border.width: 2
        }
        */

        }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值