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) }
相关推荐
枫叶丹411 分钟前
【Qt开发】信号与槽(二)-> 信号和槽的使用
开发语言·qt
Vertira1 小时前
python 阿里云 安装 dashscope的简介、安装
开发语言·python
hqxstudying3 小时前
Java异常处理
java·开发语言·安全·异常
wjs20246 小时前
状态模式(State Pattern)
开发语言
我命由我123456 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list
liulilittle6 小时前
C++ TAP(基于任务的异步编程模式)
服务器·开发语言·网络·c++·分布式·任务·tap
励志要当大牛的小白菜7 小时前
ART配对软件使用
开发语言·c++·qt·算法
爱装代码的小瓶子9 小时前
数据结构之队列(C语言)
c语言·开发语言·数据结构
Maybe_ch11 小时前
.NET-键控服务依赖注入
开发语言·c#·.net
超浪的晨11 小时前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发