Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Arc gauge



  • I kindly request some suggestions how to make a gauge that shows a value using a colored arc.
    There's a background image:
    0_1550570116302_ghiera-stato-OFF.png
    and then I have the colored arc for the inner sectors.
    0_1550570139625_1ph-stato-carica.png
    When value is 0% you will see only the background image. When value is 100% the colored arc will fill all the background one.
    For each intermediate value the colored arc will fill the background one according to the percentage.

    I don't understand how to achieve this.
    Any hint is appreciated!


  • Moderators

    @Mark81
    simply paint ieach section yourself yourself using a Canvas element:

    Canvas {
         onPaint: {
                var ctx = getContext("2d");
                ctx.resetTransform()
                ctx.reset();
    
               // do the painting on ctx
               // e.g. ctx.arc( x, y, radius, angleRadStart, angleRadStart, false)
        }
    }
    

    See this example, it's for HTML5 but it's still applicable.



  • @raven-worx I can try it but I see two major issues:

    • it would quite hard to perfectly fit each sector if they are not generated automatically using a function (like I would do with mine)
    • it would work only if the requested painting is easy to reproduce. My pictures are only an example, but the final stuff I will receive, might contains other bells & whistles like gradients along all the colored arcs, or some texture, etc...

    For these reasons I was trying to use the supplied pictures to animate the gauge.


  • Moderators

    @Mark81 said in Arc gauge:

    it would quite hard to perfectly fit each sector if they are not generated automatically using a function (like I would do with mine)

    For these reasons I was trying to use the supplied pictures to animate the gauge.

    Those 2 sentences are a contradiction no?
    actually you should only paint each sector once and color them differently according to the value.
    I mean it's way harder to place images perfectly than do custom painting at calculated coordinates.

    My pictures are only an example, but the final stuff I will receive, might contains other bells & whistles like gradients along all the colored arcs, or some texture, etc...

    So you want a solution which automagically does everything you need with least effort?

    If you want to work with images (and all it's problems, like blurring etc) the only thing you need is the Image element and probably some effects or masks.
    For a clean solution i would recommend to do the painting yourself, since this gives you also perfect placement and no problems antialiasing.



  • @raven-worx said in Arc gauge:

    Those 2 sentences are a contradiction no?

    At all.
    The pictures are generated, say, with Photoshop changing their color. So if I use them I'm sure they can overlap perfectly each other. On the other hand, if I manually draw both scales, I'm also sure they fit together but it would be harder to reproduce all the graphics effects (and the graphic guy cannot update the images himself - as he requires).

    What I meant with my sentences is it would not work if you use a mix the two approaches - as I thought (wrongly) you suggested before.

    So you want a solution which automagically does everything you need with least effort?

    Uh? I don't understand this question.
    If you see the "QtQuick control extras example" you can find how the CircularGauge is made. For the needle they used a png that rotates according to the requested value. Of course they can draw the needle by hand too, but as said this approach has several
    drawbacks. I don't think its a matter of "doing something automagically" but just to understand how to use the QML tools.



  • @raven-worx said in Arc gauge:

    If you want to work with images (and all it's problems, like blurring etc) the only thing you need is the Image element and probably some effects or masks.

    Yep, and this is the only thing I don't know how to do in this case. Hence my question :-)


  • Moderators

    @Mark81 I think, this will get complicated quite quickly, if you want to do it in pure qml & canvas.

    I would suggest looking into subclassing QQuickPaintedItem that way you can just partially draw the 2nd image, depending on your gauge value.


  • Moderators

    @Mark81 said in Arc gauge:

    Yep, and this is the only thing I don't know how to do in this case. Hence my question :-)

    assuming your images are identical in size:

    Image {
       source: "qrc/background.png"
    
       Image {
           source: "qrc:/foreground.png"
       }
    }
    


  • @J.Hilk said in Arc gauge:
    that way you can just partially draw the 2nd image, depending on your gauge value.

    Interesting. Following your approach could not be even easier to draw the colored scale into a Shape element with clip property true. The shape will be a trapezoid with an inclined side starting from the center of the gauges (bound to the value property).

    I would try it when I understand why "module "QtQuick.Shapes" version 1.12 is not installed" on my system!



  • @raven-worx this does nothing but paints one image one on top of the other....


  • Moderators

    @Mark81 said in Arc gauge:

    @raven-worx this does nothing but paints one image one on top of the other....

    i thought you get a separate image for each possible value.

    If you want to keep going the QML-only way take a look at OpacityMask
    Again for the mask shape you can use a Canvas element drawing an arc to cover the unneeded sectors.



  • @raven-worx said in Arc gauge:

    If you want to keep going the QML-only way take a look at OpacityMask
    Again for the mask shape you can use a Canvas element drawing an arc to cover the unneeded sectors.

    Got it. This is a good advice. I'm going to try it and I'll report back the result.
    Thanks for the patience!


Log in to reply