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. How can I space QML elements based upon text size?

How can I space QML elements based upon text size?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 1.5k Views 3 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.
  • RokeJulianLockhartR Offline
    RokeJulianLockhartR Offline
    RokeJulianLockhart
    wrote on last edited by RokeJulianLockhart
    #1
    Context

    With CSS3, I can apply vertical space with caps (or lhs), and widths with ems. However, with QML, all I see available is spacing, which appears to be equivalent to CSS3's px.

    The Question

    Is this an X/Y problem? Should I not space based upon text size in QML?

    A Demonstrative Example
    #!/usr/bin/env -S qml
    import QtQuick // 6.9
    import QtQuick.Controls // 6.9
    import QtQuick.Layouts // 6.9
    ApplicationWindow {
    	visible: true
    	title: qsTr("Application Menu")
    	Row {
    		ColumnLayout {
    			Label {
    				text: "Hello from QML!"
    			}
    			Button {
    				text: "Click Me"
    				onClicked: console.log("Button clicked")
    			}
    			Button {
    				text: "Click Me"
    				onClicked: console.log("Button clicked")
    			}
    			Button {
    				text: "Click Me"
    				onClicked: console.log("Button clicked")
    			}
    			Button {
    				text: "Click Me"
    				onClicked: console.log("Button clicked")
    			}
    		}
    		ColumnLayout {
    			Label {
    				text: "Hello from QML!"
    			}
    			Button {
    				text: "Click Me"
    				onClicked: console.log("Button clicked")
    			}
    			Button {
    				text: "Click Me"
    				onClicked: console.log("Button clicked")
    			}
    			Button {
    				text: "Click Me"
    				onClicked: console.log("Button clicked")
    			}
    			Button {
    				text: "Click Me"
    				onClicked: console.log("Button clicked")
    			}
    		}
    	}
    }
    
    

    When using a forum, remember to tag the person you are responding to, in case they are not subscribed to the thread.

    1 Reply Last reply
    0
    • RokeJulianLockhartR RokeJulianLockhart

      The new code doesn't set any spacing. What do you want to achieve?

      @Axel-Spoerl, I want to have the spacing be relative to the font size. To my knowledge, that is how all sizing is accomplished elsewhere. Otherwise:

      1. When I've a high-resolution display, it's going to be mighty small.

      2. My QML application's padding might differ to another's. That's poor form, because inter-application consistency matters.

      If there's a standard padding size, that could at least be a stopgap, I suppose. In CSS, this is generally defined by the platform vendors (Chromium and Gecko's development teams), so I would assume that Qt or KDE, etcetera, would provide one if this is the standard. However, this'd solely be feasible if the spacing metric scales with the display scale.

      Where is the assumption rooted, that QML is some sort of CSS syntax?

      No. It appears to be a combination of layout declaration (like HTML), style (like CSS), and code (it literally permits one to embed JS), all in one. It certainly relegates most logic to a backend, like C++ or Python, and most style to a QQuickStyle, but it appears to rely upon hardcoded paddings for most elements.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #6

      @RokeJulianLockhart said in How can I space QML elements based upon text size?:

      I want to have the spacing be relative to the font size

      FontMetrics might be your friend: https://doc.qt.io/qt-6/qml-qtquick-fontmetrics.html

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      2
      • Axel SpoerlA Offline
        Axel SpoerlA Offline
        Axel Spoerl
        Moderators
        wrote on last edited by
        #2

        I don't fully understand the problem / question.
        QML is a declarative language, CSS is a style sheet syntax. They can't really be compared to each other: The style sheet doesn't know what it will eventually be applied to. In contrast, QML declares actual controls, which hold the properties they require.

        That said:
        The code above declares a label and a button inside a row layout. Spacing is horizontal in this context. Laying out a row doesn't require vertical information. A ColumnLayout - which could hold a row layout inside - considers its spacing property to be vertical.

        Software Engineer
        The Qt Company, Oslo

        1 Reply Last reply
        0
        • RokeJulianLockhartR Offline
          RokeJulianLockhartR Offline
          RokeJulianLockhart
          wrote on last edited by RokeJulianLockhart
          #3

          The code above declares a label and a button inside a row layout. Spacing is horizontal in this context. Laying out a row doesn't require vertical information. A ColumnLayout - which could hold a row layout inside - considers its spacing property to be vertical.

          @Axel-Spoerl, my apologies. Pasted the wrong code. I've remediated it.

          QML is a declarative language, CSS is a style sheet syntax. They can't really be compared to each other: The style sheet doesn't know what it will eventually be applied to. In contrast, QML declares actual controls, which hold the properties they require.

          QML appears to be both, else it surely wouldn't permit me to declare a spacing value. As an example, HTML provides no such property; spacing is solely the purview of CSS in its context.

          Have I misunderstood? I can't imagine that I need to write a QQuickStyle merely to have some layouts have non-0 px margins, so I presume so. Perhaps, a screenshot is of use:

          A Screenshot

          I expect some default padding inside where the window decorations border (in HTML, the body), and between the columns.

          Thanks for the assistance.

          When using a forum, remember to tag the person you are responding to, in case they are not subscribed to the thread.

          1 Reply Last reply
          0
          • Axel SpoerlA Offline
            Axel SpoerlA Offline
            Axel Spoerl
            Moderators
            wrote on last edited by
            #4

            I am sorry, I still don't understand what the problem is. The new code doesn't set any spacing.
            Where is the assumption rooted, that QML is some sort of CSS syntax?
            What do you want to achieve?

            Software Engineer
            The Qt Company, Oslo

            1 Reply Last reply
            0
            • RokeJulianLockhartR Offline
              RokeJulianLockhartR Offline
              RokeJulianLockhart
              wrote on last edited by
              #5

              The new code doesn't set any spacing. What do you want to achieve?

              @Axel-Spoerl, I want to have the spacing be relative to the font size. To my knowledge, that is how all sizing is accomplished elsewhere. Otherwise:

              1. When I've a high-resolution display, it's going to be mighty small.

              2. My QML application's padding might differ to another's. That's poor form, because inter-application consistency matters.

              If there's a standard padding size, that could at least be a stopgap, I suppose. In CSS, this is generally defined by the platform vendors (Chromium and Gecko's development teams), so I would assume that Qt or KDE, etcetera, would provide one if this is the standard. However, this'd solely be feasible if the spacing metric scales with the display scale.

              Where is the assumption rooted, that QML is some sort of CSS syntax?

              No. It appears to be a combination of layout declaration (like HTML), style (like CSS), and code (it literally permits one to embed JS), all in one. It certainly relegates most logic to a backend, like C++ or Python, and most style to a QQuickStyle, but it appears to rely upon hardcoded paddings for most elements.

              When using a forum, remember to tag the person you are responding to, in case they are not subscribed to the thread.

              JKSHJ 1 Reply Last reply
              0
              • RokeJulianLockhartR RokeJulianLockhart

                The new code doesn't set any spacing. What do you want to achieve?

                @Axel-Spoerl, I want to have the spacing be relative to the font size. To my knowledge, that is how all sizing is accomplished elsewhere. Otherwise:

                1. When I've a high-resolution display, it's going to be mighty small.

                2. My QML application's padding might differ to another's. That's poor form, because inter-application consistency matters.

                If there's a standard padding size, that could at least be a stopgap, I suppose. In CSS, this is generally defined by the platform vendors (Chromium and Gecko's development teams), so I would assume that Qt or KDE, etcetera, would provide one if this is the standard. However, this'd solely be feasible if the spacing metric scales with the display scale.

                Where is the assumption rooted, that QML is some sort of CSS syntax?

                No. It appears to be a combination of layout declaration (like HTML), style (like CSS), and code (it literally permits one to embed JS), all in one. It certainly relegates most logic to a backend, like C++ or Python, and most style to a QQuickStyle, but it appears to rely upon hardcoded paddings for most elements.

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #6

                @RokeJulianLockhart said in How can I space QML elements based upon text size?:

                I want to have the spacing be relative to the font size

                FontMetrics might be your friend: https://doc.qt.io/qt-6/qml-qtquick-fontmetrics.html

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                2
                • RokeJulianLockhartR RokeJulianLockhart has marked this topic as solved on
                • RokeJulianLockhartR Offline
                  RokeJulianLockhartR Offline
                  RokeJulianLockhart
                  wrote on last edited by
                  #7

                  @JKSH, thank you! That provides a direct answer, of which spacing: fontMetrics.averageCharacterWidth appears to be the most general-purpose solution. I'm surprised that the default spacing value is 0 for so many elements, though. Perhaps, KDE's QQuickStyle lacks defaults that it should provide?

                  Regardless, is this generally how spacing is conducted in QML? If so (which I expect), I'll presume that there's some guidance documented about when/where one should utilise such values?

                  When using a forum, remember to tag the person you are responding to, in case they are not subscribed to the thread.

                  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