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) }
相关推荐
阿珊和她的猫29 分钟前
v-scale-scree: 根据屏幕尺寸缩放内容
开发语言·前端·javascript
fouryears_234173 小时前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
我好喜欢你~3 小时前
C#---StopWatch类
开发语言·c#
lifallen5 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研5 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
cui__OaO6 小时前
Linux软件编程--线程
linux·开发语言·线程·互斥锁·死锁·信号量·嵌入式学习
鱼鱼说测试7 小时前
Jenkins+Python自动化持续集成详细教程
开发语言·servlet·php
艾莉丝努力练剑7 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
CHEN5_027 小时前
【Java基础面试题】Java基础概念
java·开发语言
杜子不疼.9 小时前
《Python学习之字典(一):基础操作与核心用法》
开发语言·python·学习