Qt Quick:ComboBox 组合框

自定义ComboBox:

javascript 复制代码
import QtQuick
import QtQuick.Controls
// import QtQuick.Controls.Material

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

    ComboBox {
        id: comboBox
        width: 150; height: 35; x: 50; y: 20

        model: ListModel {
            ListElement { text: "番茄鸡蛋盖浇饭" }
            ListElement { text: "爆炒猪肝盖浇饭" }
            ListElement { text: "青椒肉丝盖浇饭" }
            ListElement { text: "粉蒸排骨" }
            ListElement { text: "青椒炒肉" }
        }

        contentItem: Text {
            text: comboBox.displayText
            verticalAlignment: Text.AlignVCenter
            // color: comboBox.pressed ? "red" : "green"
            elide: Text.ElideRight  // 内容过长时右侧省略
            leftPadding: 5
        }

        background: Rectangle {
            implicitWidth: 150; implicitHeight: 30
            color: "transparent"
            border.color: comboBox.activeFocus ? "#005BF5" : "#D9DBDE"
        }

        indicator: Image {
            width: 20; height: 20
            anchors.right: comboBox.right
            anchors.rightMargin: 5
            anchors.verticalCenter: comboBox.verticalCenter

            source: comboBox.down ? "./imgs/arrowup.png" : "./imgs/arrowdown.png"
        }

        popup: Popup {
            y: comboBox.height
            width: comboBox.width
            height: contentItem.implicitHeight
            padding: 1

            contentItem: ListView {
                clip: true
                implicitHeight: contentHeight
                model: comboBox.popup.visible ? comboBox.delegateModel : null
                currentIndex: comboBox.highlightedIndex
            }
        }
    }
}

这个ComboBox实际是由多个控件组合的,全部自定义比较复杂,我这边只是弄了个大概。

还有2个常用的信号:

javascript 复制代码
onCurrentIndexChanged: { console.log(currentIndex) }
onCurrentValueChanged: { console.log(currentValue) }
相关推荐
学逆向的3 分钟前
C 选择结构的执行效率分析
c语言·开发语言·算法·网络安全
geats人山人海33 分钟前
java 8 字符串
java·开发语言
云和数据.ChenGuang38 分钟前
Golang AI 工程师路线图:从入门到实战
开发语言·人工智能·后端·golang
事圆则缓1 小时前
Kotlin学习导航:从知识树走到项目与面试
android·开发语言·kotlin
kunicoding1 小时前
C++ 策略模式实战:写一个可以“一键换输出“的日志系统
开发语言·c++·策略模式
纪念 2291 小时前
C++算法(三)
开发语言·c++·算法
csdn_zhangchunfeng1 小时前
linux 如何安装rst2man命令
开发语言·chrome·python
sejinan1 小时前
新版!IPTV直播源列表 V1.7 正式发布:接口大厅与会员定制生态解读
开发语言·php
我喜欢就喜欢1 小时前
DeepSeek Harness 开源贡献手记:从零到合入主线的完整历程
开发语言·人工智能·copilot
“AI国潮设计-小江”2 小时前
《【AIGC实战】Python+SDXL批量生成“猫咪英歌舞蛋挞”IP,附保持角色一致性的代码逻辑》
开发语言·人工智能·python·prompt·aigc