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) }
相关推荐
南 阳15 分钟前
Python从入门到精通day56
开发语言·python
m0_5698814721 分钟前
C++中的组合模式高级应用
开发语言·c++·算法
m0_7301151126 分钟前
高性能计算负载均衡
开发语言·c++·算法
孞㐑¥34 分钟前
算法—记忆化搜索
开发语言·c++·经验分享·笔记·算法
xushichao198935 分钟前
代码覆盖率工具实战
开发语言·c++·算法
2401_8579182936 分钟前
C++与WebAssembly集成
开发语言·c++·算法
2401_8796938737 分钟前
C++与微服务架构
开发语言·c++·算法
阿kun要赚马内38 分钟前
Python中函数的进阶用法
开发语言·python
Spliceㅤ41 分钟前
项目:基于qwen的点餐系统
开发语言·人工智能·python·机器学习·自然语言处理
ZHOUPUYU1 小时前
PHP与WebSocket实时通信的原理到生产级应用
开发语言·html·php