[Development] Scalable UIs in QtQuick (take 2)

Dominik Haumann dhaumann at kde.org
Thu Feb 18 23:08:22 CET 2016


On Thu, Feb 18, 2016 at 11:50 AM, Hausmann Simon
<Simon.Hausmann at theqtcompany.com> wrote:
> Hi,
>
> A little while ago Lars and I proposed to introduce physical units in the QML language for use in QtQuick. The idea was to make it easier to write user interfaces that adapt to different display resolutions by "pinning" your UI to physical dimensions. For example you could say
>
>     Image {
>         source: "mypprofilephoto.png"
>         width: 5cm
>         height: 5cm
>     }

Interestingly, I once wrote a Value class [1] that does exactly this.
It allows to have
constructs like this:

Value v1(2, Unit::Centimeter);
Value v2 = Value::fromString("1in)"; // see also: toString()
Value v3 = v1 * v2; // Restult is 4.45 cm
bool v2GreaterThanv1 = v2 > v1; // = true

Value v4(0, Unit::Millimeter);
Value v5 = v1 / v4; // v5.isValid() == false

I use this in tikzkit, a wysiwyg pgf/TikZ LaTeX diagram editor, to
show the perfectly
correct sizes on screen (works really well). The Value class supports
only pt, cm,
mm, and inch (could of course be extended. And conversion is fast, see the
lookup implementation). Having this as integral part of the
application. I never have
to worry about unit conversion errors, since the result is always
correct, and the
unit type is even preserved when the user explicitly typed something in cm or
inch etc.

So from my experience, writing this Value class was a very good decision.
Although I personally don't program much in QML, I still believe that something
like this (or similar) is probably worth it (in QML and probably overall in Qt).

Based on this, I also have a Pos class, that hast two Value members:
x() and y(),
so all what I said above is also valid for 2D positions (see [2]).

Cheers,
Dominik

[1] https://github.com/dhaumann/tikzkit/blob/master/src/core/utils/Value.h
[2] https://github.com/dhaumann/tikzkit/blob/master/src/core/utils/Pos.h



More information about the Development mailing list