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

演示

相关推荐
✎ ﹏梦醒͜ღ҉繁华落℘1 小时前
FreeRTOS学习笔记(应用)-- 各种 信号量的应用场景
笔记·学习
星星火柴9361 小时前
笔记 | C++面向对象高级开发
开发语言·c++·笔记·学习
BeingACoder1 小时前
【SAA】SpringAI Alibaba学习笔记(一):SSE与WS的区别以及如何注入多个AI模型
java·笔记·学习·saa·springai
安全不再安全2 小时前
免杀技巧 - 早鸟注入详细学习笔记
linux·windows·笔记·学习·测试工具·web安全·网络安全
BreezeJuvenile2 小时前
外设模块学习(8)——HC-SR04超声波模块(STM32)
stm32·单片机·嵌入式硬件·学习·超声波测距模块·hc-sr04
LBuffer3 小时前
破解入门学习笔记题三十八
笔记·学习
微露清风3 小时前
系统性学习C++-第十讲-stack 和 quene
java·c++·学习
PyAIGCMaster3 小时前
钉钉的设计理念方面,我可以学习
人工智能·深度学习·学习·钉钉
Elias不吃糖4 小时前
第四天学习总结:C++ 文件系统 × Linux 自动化 × Makefile 工程化
linux·c++·学习
雍凉明月夜4 小时前
人工智能学习中深度学习之python基础之 类
python·学习