Qt quick controls ListView size issue

1.8k views Asked by At

I have some problem with popup window size behavior using Qt Quick Controls 2. When I'm putting ListView as contentItem for Popup window, Popup window size is zero. Some sample code that reproduce issue:

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
                }
            }
        }
    }
}

How to make popup adopt to the size of ListView?

1

There are 1 answers

0
jpnurmi On BEST ANSWER

A vertical ListView does not provide content width. It is always -1. You'll have to specify something, for example:

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

    contentItem: ListView {
        implicitWidth: 200 // <==
        implicitHeight: contentHeight
        //...
    }
}