Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-122000

Implement support for units (px, pt, percent etc) in QML

    XMLWordPrintable

Details

    Description

      Ideally we want to write something like:

      Text {
          fontSize: 12pt
      }
      

      How do we get there? Already today we can do the following:

      1. Write constructible value types called "pt" and "px" that can be constructed from a number.
      2. Write a value type called "screenRelatedSize" (please bikeshed the name) and make it constructible from pt or px, basically a tagged union over those, which can be resolved into some physical size if given a dpi value.
      3. Add a fontSize property of type screenRelatedSize to Text and have it handle whatever that produces.

      Now you can write:

      Text {
          fontSize: 12 as pt
      }
      

      That's already not all that bad.

      To improve on this even further, we could add a syntactical hack to the QML parser so that a number directly followed by a lowercase identifier (without space in between) is interpreted as an "as" expression. This should be possible because such a thing is currently invalid code AFAICS. With that, we can have the original form.

      An argument can be had that we shouldn't actually squat such a prominent syntactical construct in case JavaScript wants to add it it itself, with different semantics. In that case we may want to either opt into this using a pragma or offer some less obvious syntax like "12_pt".

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              qtqmlteam Qt Qml Team User
              ulherman Ulf Hermann
              Votes:
              2 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes