Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QPopup auto size to TableView
Forum Updated to NodeBB v4.3 + New Features

QPopup auto size to TableView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmltableviewpopup
1 Posts 1 Posters 166 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matthew Jones
    wrote on 30 Oct 2024, 13:30 last edited by Matthew Jones
    #1

    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!

    1 Reply Last reply
    0

    1/1

    30 Oct 2024, 13:30

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved