Qt/QML学习-自定义CheckBox

QML学习

main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

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

    CheckBox {
        id: checkBox
        font.pointSize: 30
        text: "放假当宅宅"
        tristate: true
        onCheckStateChanged: {
            switch (checkState) {
            case Qt.Checked:
                logText.text = text+":\nChecked"
                break
            case Qt.PartiallyChecked:
                logText.text = text+":\nPartiallyChecked"
                break
            default:
                logText.text = text+":\nUnChecked"
                break
            }
        }
        background: Rectangle {
            color: "blue"
            width: checkBox.implicitWidth
        }
        contentItem: Text {
            text: checkBox.text
            color: "yellow"
            font.pixelSize: 30
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            leftPadding: checkBox.indicator.width
        }
        indicator: Rectangle {
            color: "transparent"
            width: image.width
            height: checkBox.height
            Image {
                id: image
                anchors.centerIn: parent
                height: checkBox.height
                source: checkBox.getCheckState()
                fillMode: Image.PreserveAspectFit
            }
        }
        function getCheckState() {
            switch (checkBox.checkState) {
            case Qt.Checked:
                return "qrc:/Checked.png"
            case Qt.PartiallyChecked:
                return "qrc:/PartiallyChecked.png"
            default:
                return "qrc:/UnChecked.png"
            }
        }
    }

    Rectangle {
        anchors.top: checkBox.bottom
        width: logText.width
        height: logText.height
        color: "yellow"
        Text {
            id: logText
            font.pointSize: 30
            text: "点赞+关注"
        }
    }
}

演示

相关推荐

  1. Qt/QML学习-定义CheckBox

    2024-07-15 10:54:04       26 阅读
  2. html定义禁用状态下且已选中的checkbox

    2024-07-15 10:54:04       32 阅读
  3. flutter学习-day16-定义组件

    2024-07-15 10:54:04       60 阅读
  4. NebulaGraph学习笔记-定义池连接

    2024-07-15 10:54:04       41 阅读
  5. 机器学习复习(9)——定义dataset

    2024-07-15 10:54:04       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 10:54:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 10:54:04       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 10:54:04       58 阅读
  4. Python语言-面向对象

    2024-07-15 10:54:04       69 阅读

热门阅读

  1. Django会话机制

    2024-07-15 10:54:04       21 阅读
  2. 计算机网络 TCP粘包问题

    2024-07-15 10:54:04       23 阅读
  3. 洛谷P8839~8841题解

    2024-07-15 10:54:04       19 阅读
  4. 机器学习-16-分布式梯度提升库XGBoost的应用

    2024-07-15 10:54:04       27 阅读
  5. hot100 | 九、图论

    2024-07-15 10:54:04       26 阅读
  6. day2 上下文Context

    2024-07-15 10:54:04       22 阅读
  7. 重学PyTorch,粗略笔记(一)

    2024-07-15 10:54:04       23 阅读
  8. 序列标注任务 - CRF条件随机场

    2024-07-15 10:54:04       18 阅读
  9. Python 字典(Dict)详解与实战应用

    2024-07-15 10:54:04       22 阅读
  10. 翁恺-C语言程序设计-07-3. 数素数

    2024-07-15 10:54:04       23 阅读