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
    }
}

演示

相关推荐
白帽小阳21 分钟前
我的AI辅助开发工具链2026版:从编码到部署的全栈智能实践
网络·人工智能·学习·安全·自动化
有Li1 小时前
通过渐进式域变换实现组织分类的无监督单域泛化文献速递/基于多模态的医学影像分割与理解
人工智能·学习·分类·数据挖掘·文献·医学生
kdxiaojie1 小时前
Linux 驱动研究 —— V4L2 (2)
linux·运维·笔记·学习
神秘的MT1 小时前
SQL Server 分页查询 ROW_NUMBER () OFFSET...FETCH游标(Cursor) 数据插入
数据库·sql·学习·c#·winform
~kiss~2 小时前
大模型的PEFT~ 参数高效微调 Parameter-Efficient Fine-Tuning
学习
YUS云生3 小时前
Python学习笔记·第32天:FastAPI进阶——响应类型、中间件与依赖注入
笔记·python·学习
minglie13 小时前
zynq中axi_dma的三个实验-第1课:PL搭建
学习
xianrenli383 小时前
Android14自动时区更新
android·学习·framework·aosp14·自动时区
MartinYeung53 小时前
[论文学习]Unsafe LLM-Based Search: AI搜索引擎安全风险的定量分析与缓解
人工智能·学习·搜索引擎
心中有国也有家3 小时前
AtomGit Flutter 鸿蒙客户端:为 E-Brufen 注入情感节律的文案设计哲学
javascript·学习·flutter·华为·harmonyos