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

演示

相关推荐
禹中一只鱼4 小时前
【力扣热题100学习笔记】 - 哈希
java·学习·leetcode·哈希算法
SteveSenna6 小时前
项目:Trossen Arm MuJoCo
人工智能·学习·算法
m0_747304166 小时前
GNN学习
学习
Sagittarius_A*6 小时前
监督学习(Supervised Learning)
人工智能·学习·机器学习·监督学习
cpp_learners7 小时前
银河麒麟V10+飞腾FT-2000/4处理器+QT源码静态编译5.14.2指南
开发语言·qt
qqty12177 小时前
Java进阶学习之路
java·开发语言·学习
WHS-_-20227 小时前
Python 算法题学习笔记一
python·学习·算法
_李小白7 小时前
【OSG学习笔记】Day 22: StateSet 与 StateAttribute (渲染状态)
笔记·学习
GIS阵地8 小时前
QgsProviderMetadata 详解(基于 QGIS 3.40.13 API)
数据库·qt·arcgis·oracle·gis·开源软件·qgis
TorrieLeung8 小时前
碎片学习|外贸tob 一手交钱一手交货
学习·tob·外贸销售·工地英语