2
votes

I would like to set the minimum width and height of my QML Application window, so that the content item is fully visible (not clipped).

import QtQuick 2.5
import QtQuick.Controls 1.4

ApplicationWindow {
    visible: true
    width: 100
    height: 100

    title: "test"

    minimumWidth: circle.width
    minimumHeight: circle.height // + menuBar.height

    menuBar: MenuBar {
        Menu {
            title: qsTr("File")
            MenuItem {
                text: qsTr("Exit")
                onTriggered: Qt.quit();
            }
        }
    }

    Rectangle {
        id: circle
        anchors.centerIn: parent
        width: 200
        height: 200
        color: "red"
        radius: width * 0.5
    }
}

Here is the result:

enter image description here

As you can see, setting the minimum width works fine. The minimum height seems to be off by the height of the menu bar. The problem is, adding something like menuBar.height does not work as this property does not exist.

So the question is: how do I set the size of the ApplicationWindow, so that the content item (given by width/height or implicitWidth/implicitHeight) is not clipped?

Note: In reality, instead of a red circle, the content item serves as a game canvas, which I would like to resize dynamically.

1

1 Answers

3
votes

As always with the old QtQuick.Controls 1.x the only way to help yourself is, to look at the (undocumented/internal) properties. For the MenuBar those are:

  • objectName
  • menus
  • __contentItem
  • __parentWindow
  • __isNative
  • style
  • __style
  • __menuBarComponent
  • objectNameChanged
  • menusChanged
  • nativeChanged
  • contentItemChanged
  • styleChanged
  • __styleChanged
  • __menuBarComponentChanged

__contentItem seems to be interesting, and it features a height - as soon as it is instantiated.

So we can define the height of the ApplicationWindow as such:

minimumHeight: contentItem.childrenRect.height
               + (menuBar.__contentItem ? menuBar.__contentItem.height : 0)