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) }
相关推荐
李少兄9 天前
解决OSS存储桶未创建导致的XML错误
xml·开发语言·python
阿蒙Amon9 天前
《C#图解教程 第5版》深度推荐
开发语言·c#
学Linux的语莫9 天前
python基础语法
开发语言·python
mahuifa9 天前
PySide环境配置及工具使用
python·qt·环境配置·开发经验·pyside
小灰灰搞电子9 天前
Qt PyQt与PySide技术-C++库的Python绑定
c++·qt·pyqt
暖馒9 天前
C#委托与事件的区别
开发语言·c#
嘉琪0019 天前
2025——js 面试题
开发语言·javascript·ecmascript
Jinxiansen02119 天前
Vue3 中 ref 与 reactive 使用场景总结(含对比与示例)
开发语言·javascript·ecmascript
时空自由民.9 天前
C++ 不同线程之间传值
开发语言·c++·算法
止观止9 天前
Rust智能指针演进:从堆分配到零复制的内存管理艺术
开发语言·后端·rust