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. Specifying both size and implicit size in QML
Forum Updated to NodeBB v4.3 + New Features

Specifying both size and implicit size in QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 2.7k 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.
  • P Offline
    P Offline
    Puya
    wrote on last edited by Puya
    #1

    Hi,

    What are the effects of specifying both height/width and implicitHeight/implicitWidth? I have seen this done in some of the examples, for example this one:
    qtdeclarative/examples/quick/shared/CheckBox.qml

    My interpretation of the documentation is that if no size is specified, then implicit size is used. And if size IS specified, then it is overwritten if the user of the component also specifies the size.

    So what would be the effect of specifying size, when implicit size is already specified? Can you show me an example usage were having / not having the size would make a difference?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Puya
      wrote on last edited by
      #2

      From Qt example code:
      https://code.woboq.org/qt5/qtdeclarative/examples/quick/shared/Button.qml.html
      https://code.woboq.org/qt5/qtdeclarative/examples/quick/shared/CheckBox.qml.html

      1 Reply Last reply
      0
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by GrecKo
        #3

        Doing

        width: implicitWidth
        height: implicitHeight
        

        like in the example you mentionned is useless. Like you said the width and height will be the same as their implicit counterpart if not specified, that's a feature of QQuickItem/Item.

        Generally you don't want to specify the explicit size (with width/height or anchors) of a root Item.
        My rule of thumb is setting the implicit size of parent based on the implicit sizes of it children and the explicit size of children based on the explicit size of its parent.

        This allow to specify a sensible default size for a component with children that still can be resizable.

        Example to illustrate this :

        Rectangle {
            implicitWidth: textItem.implicitWidth
            implicitHeight: textItem.implicitHeight
            Text {
                id: textItem
                width: parent.width
                height: parent.height
                // or anchors.fill: parent
            }
        }
        
        1 Reply Last reply
        3
        • P Offline
          P Offline
          Puya
          wrote on last edited by Puya
          #4

          Thanks @GrecKo . An example with both size and implicit size for inner items of a component for anyone else who is interested:

          // GreenFrame.qml
          import QtQuick 2.15
          Item {
              id: root
              implicitHeight: frame.implicitHeight
              implicitWidth: frame.implicitWidth
              Rectangle {
                  id: frame
                  color: "green"
                  height: root.height
                  width: root.width
                  // or equivalently anchors.fill: parent
                  implicitHeight: centerImage.implicitHeight * 2
                  implicitWidth: centerImage.implicitWidth * 2
                  Image {
                      id: centerImage
                      source: "image.png"
                      height: frame.height / 2
                      width: frame.width / 2
                      anchors.centerIn: parent
                      fillMode: Image.Stretch
                  }
              }
          }
          
          1 Reply Last reply
          0

          • Login

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