QPopup auto size to TableView
Unsolved
QML and Qt Quick
-
I am struggling with a basic problem: how to have a QPopup containing a TableView, and automatically sizing to the table.
This basic example doesn't work. It is basically a couple of Qt examples bolted together.
All I get is a small white square - presumably the 12x12 popup border with no content.
According to https://doc.qt.io/qt-6/qml-qtquick-controls-popup.html#popup-sizing the popup should automatically size to the table.
The table should know its size - the delegate is trivial.
How do I make this work? I want the popup to only be as big as the table, not fixed size.import QtQuick.Controls 2.15 import QtQuick import Qt.labs.qmlmodels Popup { id: dlg // These make no difference: // contentWidth: view.implicitWidth // contentHeight: view.implicitHeight Component.onCompleted: report("completed") onImplicitHeightChanged: report("implH") onImplicitWidthChanged: report("implW") onHeightChanged: report("w") onWidthChanged: report("h") onVisibleChanged: report("visible") function report(context) { console.log("Popup", context, visible, width, "x", height, "implicit:", implicitWidth, "x", implicitHeight) } TableView { id: view model: TableModel { TableModelColumn { display: "name" } TableModelColumn { display: "color" } rows: [ { "name": "cat", "color": "black" }, { "name": "dog", "color": "brown" }, { "name": "bird", "color": "white" } ] } delegate: Rectangle { implicitWidth: 100 implicitHeight: 50 border.width: 1 color: "red" Text { text: display anchors.centerIn: parent } } Component.onCompleted: report("completed") onImplicitHeightChanged: report("implH") onImplicitWidthChanged: report("implW") onHeightChanged: report("h") onWidthChanged: report("w") onVisibleChanged: report("visible") function report(context) { console.log("TableView", context, visible, width, "x", height, "implicit:", implicitWidth, "x", implicitHeight, "model", model.columnCount, "x", model.rowCount) } } }
The debug I get is:
Startup:qml: TableView visible false 0 x 0 implicit: 0 x 0 model 0 x 0 qml: Popup w false 12 x 12 implicit: 12 x 12 qml: Popup implH false 12 x 12 implicit: 12 x 12 qml: Popup h false 12 x 0 implicit: 12 x 12 qml: Popup implW false 12 x 12 implicit: 12 x 12 qml: Popup completed false 12 x 12 implicit: 12 x 12 qml: TableView completed false 0 x 0 implicit: 0 x 0 model 2 x 3
When the popup is open()ed:
qml: TableView visible true 0 x 0 implicit: 0 x 0 model 2 x 3 qml: Popup visible true 12 x 12 implicit: 12 x 12
Thanks!