Quick Controls --1.button

1 results

Here Insert Picture Description

2 Introduction

The most commonly used key button, button made of Control2 fixed in the package, to allow more flexible use of custom button assembly.

3 control code

3.1 SenComBtn.qml

import QtQuick 2.12

Rectangle {
    // my feature
    property string btnMsg: ""
    property string btnText: ""

    property color textColor: "#ff000000"
    property string pressedTextColor: textColor
    property string releaseTextColor: textColor

    property real fontSize: 20

    property string btnColor: "#F3F3F3"
    property string pressedColor: btnColor
    property string releaseColor: btnColor

    property string borderColor: textColor
    property string pressedBorderColor: pressedTextColor

    property alias wrapMode: textId.wrapMode
    property alias elide: textId.elide

    width: 80; height: 40; radius: 15
    color: mouseArea.pressed ? pressedColor : releaseColor
    border.width: 0
    border.color: mouseArea.pressed ? pressedBorderColor : borderColor
    focus: true
    signal clicked()
    // signal clickedWithMsg(String msg)
    signal pressed()
    signal release()

    Text {
        id: textId
        anchors.verticalCenter: parent.verticalCenter
        anchors.horizontalCenter: parent.horizontalCenter
        wrapMode: Text.WordWrap
        text: btnText
        color: mouseArea.pressed ? pressedTextColor : releaseTextColor
        font.pixelSize: fontSize
    }
    MouseArea {
        id: mouseArea
        hoverEnabled: true
        anchors.fill: parent
        onClicked: {
            parent.clicked()
            // parent.clickedWithMsg(btnMsg)
        }
        onPressed: {
            parent.pressed(btnMsg)
        }
        onReleased: {
            parent.release()
        }
    }
}

3.2 main.qml

import QtQuick 2.12
import QtQuick.Window 2.12
import "./common" as SenCom

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    SenCom.SenComBtn {
        id: fileBtn
        anchors.centerIn: parent
        btnText: "Sen"
        pressedColor: "lightgray"
    }
}
Published 496 original articles · won praise 601 · Views 1.55 million +

Guess you like

Origin blog.csdn.net/qq_38880380/article/details/104353963