0

我对使用 Qt Quick Controls 2 的弹出窗口大小行为有一些问题。当我将 ListView 作为弹出窗口的 contentItem 时,弹出窗口大小为零。一些重现问题的示例代码:

import QtQuick 2.4
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

ApplicationWindow {
    id: window
    visible: true
    width: 800
    height: 600

    Button {
        text: "open popup"
        onClicked: popup.open()
    }

    Popup {
        id: popup
        x: (window.width - width) / 2
        y: window.height / 6
        width: contentWidth
        height: contentHeight

        contentItem: ListView {
            width: contentWidth
            height: contentHeight
            model: ListModel {
                ListElement {
                    name: "Apple"
                    cost: 2.45
                }
                ListElement {
                    name: "Orange"
                    cost: 3.25
                }
                ListElement {
                    name: "Banana"
                    cost: 1.95
                }
            }

            delegate: RowLayout {
                Label {
                    text: name
                }
                Label {
                    text: cost
                }
            }
        }
    }
}

如何使弹出窗口适应 ListView 的大小?

4

1 回答 1

6

垂直ListView不提供内容宽度。它总是-1。您必须指定一些内容,例如:

Popup {
    id: popup
    x: (window.width - width) / 2
    y: window.height / 6

    contentItem: ListView {
        implicitWidth: 200 // <==
        implicitHeight: contentHeight
        //...
    }
}
于 2016-12-28T17:18:21.213 回答