Qt/QML学习-ComboBox

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("ComboBox")

    ComboBox {
        id: comboBox
        // 列表项数据模型
        model: ListModel {
            ListElement { name: "点赞"; color: "black"}
            ListElement { name: "关注"; color: "red" }
            ListElement { name: "投币"; color: "blue" }
        }
        // 显示文本对应的数据模型属性
        textRole: "name"
        // 选项改变
        onCurrentTextChanged: {
            logText.text = currentText
        }
        // ComboBox内容视图
        contentItem: Text {
            font.pointSize: 30
            text: comboBox.displayText
            color: "green"
            leftPadding: 10
        }
        // ComboBox背景视图
        background: Rectangle {
            color: "yellow"
            border.width: 1
        }
        // 选项视图代理
        delegate: ItemDelegate {
            width: comboBox.width
            height: comboBox.height
            // 选项内容视图
            contentItem: Text {
                text: name
                font.pointSize: 30
                color: model.color
                verticalAlignment: Text.AlignVCenter
            }
            // 选项背景视图
            background: Rectangle {
                color: comboBox.highlightedIndex === index?
                           "yellow": "transparent"
            }
        }
        // 指示器
        indicator: Rectangle {
            x: comboBox.width - width
            y: 0
            width: 50
            height: comboBox.height
            color: "transparent"
            Canvas {
                anchors.fill: parent
                onPaint: {
                    var ctx = getContext('2d')
                    ctx.beginPath()
                    ctx.moveTo(10, 10)
                    ctx.lineTo(40, 10)
                    ctx.lineTo(25, height-10)
                    ctx.closePath()
                    ctx.fillStyle = "blue"
                    ctx.fill()
                    ctx.strokeStyle = "black"
                    ctx.lineWidth = 2
                    ctx.stroke()
                }
            }
        }
    }

    // 演示
    Text {
        id: logText
        anchors.left: comboBox.right
        anchors.leftMargin: 20
        font.pointSize: 30
    }
}

演示

相关推荐
尤老师FPGA29 分钟前
QT代码自适应窗口
开发语言·qt
吃好睡好便好42 分钟前
矩阵旋转的计算
学习·线性代数·算法·矩阵
江华森1 小时前
TCP/IP 网络协议学习笔记(含上机实操)
网络协议·学习·tcp/ip
「維他檸檬茶」1 小时前
记录python学习(未完待续)
学习·语言
玹之又玹1 小时前
AI 入门知识点学习扫盲
人工智能·学习·ai
_橙时_1 小时前
【学习记录01】
学习·em
小陈phd1 小时前
多模态大模型学习笔记(四十五)——视觉推理(Visual Reasoning):从观察到逻辑的复杂认知链
人工智能·笔记·学习
旺仔Sec1 小时前
【AI数字营销新纪元】解锁流量新密码:CSDN“GEO工具”全网首测,让你的文章被大模型“记住”!
人工智能·学习·用户体验
呼Lu噜1 小时前
Vue3学习-Vue3概述&项目创建
学习
Upsy-Daisy1 小时前
IOTA 学习笔记(八):本地启动 IOTA Localnet
笔记·学习