StackLayout children have unpredictable widths
-
Hi all -
I'm trying to get my app working in full screen mode, and I've run into something I can't explain: two (seemingly equivalent) children of a StackLayout are getting different widths. Here's the code to reproduce it:
// Main.qml import QtQuick import QtQuick.Controls import QtQuick.Layouts ApplicationWindow { id: mainWindow visibility: Window.AutomaticVisibility visible: true flags: Qt.FramelessWindowHint | Qt.Window width: Screen.width height: Screen.height ColumnLayout { anchors.fill: parent spacing: 0 Maincontent {} } }
// Maincontent.qml import QtQuick import QtQuick.Layouts StackLayout { id: mainContent Layout.fillHeight: true Layout.fillWidth: true SpacesScreen { id: spacesScreen } EquipmentScreen { id: equipmentScreen } }
// SpacesScreen.qml import QtQuick import QtQuick.Controls Pane { id: spacesPane Component.onCompleted: { console.log("SpacesScreen.qml: spacesPane.width is " + spacesPane.width) } }
// EquipmentScreen.qml import QtQuick import QtQuick.Controls Pane { id: equipmentPane Component.onCompleted: { console.log("EquipmentScreen.qml: equipmentPane.width is " + equipmentPane.width) } }
Here's the output:
qml: EquipmentScreen.qml: equipmentPane.width is 24 // WRONG qml: SpacesScreen.qml: spacesPane.width is 1920
I've discovered that if I add this line in my use of EquipmentScreen, I get the desired answer:
EquipmentScreen { id: equipmentScreen implicitWidth: mainContent.width // this makes it work OK. }
But I don't understand why I should have to do that. Can anyone explain what's going on here? I've tried Qt 6.5.3 and 6.7.1 with the same results.
Thanks...
-
-
If width or height is not specified, an item's effective size will be determined by its implicitWidth or implicitHeight. However, if an item is the child of a layout, the layout will determine the item's preferred size using its implicit size. In such a scenario, the explicit width or height will be ignored.
-
-
@GrecKo said in StackLayout children have unpredictable widths:
Is the with of equipmentPane always wrong or only in inCompleted?
I'm not sure...the behavior now seems to be somewhat unpredictable.
@GrecKo said in StackLayout children have unpredictable widths:
The StackLayout might take a couple of frames to settle on its children size.
Ah. So, what does one do to obviate this?
-
M mzimmers has marked this topic as solved on