如何在 QML 中更改 Popup contentItem 中的 Listview 背景颜色

how to change Listview background color inside Popup contentItem in QML

我正在使用 QML 网站上关于如何自定义 ComboBox 的示例,如下所示:

#Combo.qml

import QtQuick 2.12
import QtQuick.Controls 2.12

ComboBox {
    id: control
    model: ["First", "Second", "Third"]

    delegate: ItemDelegate {
        width: control.width
        contentItem: Text {
            text: modelData
            color: "#21be2b"
            font: control.font
            elide: Text.ElideRight
            verticalAlignment: Text.AlignVCenter
        }
        highlighted: control.highlightedIndex === index
    }

    indicator: Canvas {
        id: canvas
        x: control.width - width - control.rightPadding
        y: control.topPadding + (control.availableHeight - height) / 2
        width: 12
        height: 8
        contextType: "2d"

        Connections {
            target: control
            function onPressedChanged() { canvas.requestPaint(); }
        }

        onPaint: {
            context.reset();
            context.moveTo(0, 0);
            context.lineTo(width, 0);
            context.lineTo(width / 2, height);
            context.closePath();
            context.fillStyle = control.pressed ? "#17a81a" : "#21be2b";
            context.fill();
        }
    }

    contentItem: Text {
        leftPadding: 0
        rightPadding: control.indicator.width + control.spacing

        text: control.displayText
        font: control.font
        color: control.pressed ? "#17a81a" : "#21be2b"
        verticalAlignment: Text.AlignVCenter
        elide: Text.ElideRight
    }

    background: Rectangle {
        implicitWidth: 120
        implicitHeight: 40
        border.color: control.pressed ? "#17a81a" : "#21be2b"
        border.width: control.visualFocus ? 2 : 1
        radius: 2
    }

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

        contentItem: ListView {
            clip: true
            implicitHeight: contentHeight
            model: control.popup.visible ? control.delegateModel : null
            currentIndex: control.highlightedIndex
            // ADDED SECTION TO CHANGE BACKGROUND OF LISTVIEW
            delegate: Rectangle {
                color: "#080808"
                Text {
                    anchors.fill: parent
                    text: modelData
                }
             }
             ///////////////////////////////////////////////////

            ScrollIndicator.vertical: ScrollIndicator { }
        }

        background: Rectangle {
            color: "#080808"
            radius: 2
        }
    }
}

#main.qml

import QtQuick 2.15
import QtQuick.Window 2.15

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


    Combo {
        width: 200
        height: 40
        anchors.centerIn: parent
    }
}

我在 Combo.qml 中添加了一些新代码,如上所示,将 ListView 项目的背景变为较深的颜色以匹配背景颜色Popup 本身,但没有任何变化。 ListView 项目的背景颜色始终为白色。我会很感激一些帮助来解决这个问题。谢谢

目前有两个问题。首先,comboBox 上的 delegate: 显然优先于 ListView 上的 delegate:。其次,ItemDelegate 有一些非常具体的突出显示行为,因此您需要像这样覆盖它的背景和着色行为:

import QtQuick 2.12
import QtQuick.Controls 2.12

ComboBox {
    id: control
    model: ["First", "Second", "Third"]

    delegate: ItemDelegate {
        id: itemDelegate
        width: control.width
        background: Rectangle {
            visible: itemDelegate.down || itemDelegate.highlighted || itemDelegate.visualFocus
            color: itemDelegate.highlighted ? "#808080" : "#080808"
            implicitWidth: 100
            implicitHeight: 40
        }

        contentItem: Text {
            text: modelData
            color: "#21be2b"
            font: control.font
            elide: Text.ElideRight
            verticalAlignment: Text.AlignVCenter
        }
        highlighted: control.highlightedIndex === index
    }

    indicator: Canvas {
        id: canvas
        x: control.width - width - control.rightPadding
        y: control.topPadding + (control.availableHeight - height) / 2
        width: 12
        height: 8
        contextType: "2d"

        Connections {
            target: control
            function onPressedChanged() { canvas.requestPaint(); }
        }

        onPaint: {
            context.reset();
            context.moveTo(0, 0);
            context.lineTo(width, 0);
            context.lineTo(width / 2, height);
            context.closePath();
            context.fillStyle = control.pressed ? "#17a81a" : "#21be2b";
            context.fill();
        }
    }

    contentItem: Text {
        leftPadding: 0
        rightPadding: control.indicator.width + control.spacing

        text: control.displayText
        font: control.font
        color: control.pressed ? "#17a81a" : "#21be2b"
        verticalAlignment: Text.AlignVCenter
        elide: Text.ElideRight
    }

    background: Rectangle {
        implicitWidth: 120
        implicitHeight: 40
        border.color: control.pressed ? "#17a81a" : "#21be2b"
        border.width: control.visualFocus ? 2 : 1
        radius: 2
    }

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

        contentItem: ListView {
            clip: true
            implicitHeight: contentHeight
            model: control.popup.visible ? control.delegateModel : null
            currentIndex: control.highlightedIndex
            // ADDED SECTION TO CHANGE BACKGROUND OF LISTVIEW
//            delegate: Rectangle {
//                width: parent.width
//                height: 40
//                color: "#080808"
//                Text {
//                    anchors.fill: parent
//                    text: modelData
//                }
//             }
             ///////////////////////////////////////////////////

            ScrollIndicator.vertical: ScrollIndicator { }
        }

        background: Rectangle {
            color: "#080808"
            radius: 2
        }
    }

}