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. StackLayout children have unpredictable widths
Qt 6.11 is out! See what's new in the release blog

StackLayout children have unpredictable widths

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 663 Views 1 Watching
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

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

    GrecKoG 1 Reply Last reply
    0
    • mzimmersM mzimmers

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

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #3

      @mzimmers Is the with of equipmentPane always wrong or only in inCompleted? The StackLayout might take a couple of frames to settle on its children size.

      @JoeCFD In a StackLayout, children fill the layout by default.

      mzimmersM 1 Reply Last reply
      0
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #2

        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.

        1 Reply Last reply
        0
        • mzimmersM mzimmers

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

          GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #3

          @mzimmers Is the with of equipmentPane always wrong or only in inCompleted? The StackLayout might take a couple of frames to settle on its children size.

          @JoeCFD In a StackLayout, children fill the layout by default.

          mzimmersM 1 Reply Last reply
          0
          • GrecKoG GrecKo

            @mzimmers Is the with of equipmentPane always wrong or only in inCompleted? The StackLayout might take a couple of frames to settle on its children size.

            @JoeCFD In a StackLayout, children fill the layout by default.

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #4

            @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?

            1 Reply Last reply
            0
            • mzimmersM mzimmers has marked this topic as solved on

            • Login

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