Results 1 to 3 of 3

Thread: Knobs effect in QML

  1. #1
    Join Date
    Nov 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Knobs effect in QML

    Hi all,

    I’m trying to implement a custom widget in QML to obtain an effect such as http://anthonyterrien.com/knob/ [× Angle offset element in the page].
    I tried with canvas object but if I apply a scale to the parent object of the canvas the quality of the canvas appear very ugly.
    I tried with a glsl frag shader but the variable glFragCoord bind me to the screen coordinates and anyway if I apply a scale to the parent object the rendering appears ugly.

    Someone can help me please???

  2. #2
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Knobs effect in QML

    What do you mean by ugly ?
    To enable antialiasing in Qt Quick Canvas , you must set the property " antialiasing " to "true" and set the property "renderTarget" to "Canvas.Image"
    [find on internet]

  3. #3
    Join Date
    Nov 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Knobs effect in QML

    Hi Bounce,

    thanks for your reply. In order to see the ugly effect of the canvas object, I modified the example project "canvas" that comes with the Qt 5.1.1 release. I have added another slider control, to change the PARENT scale. Obviously, by changing the parent scale, all the scene objects are affected from the new scale value. When this scale value change, the canvas object does not rendering well, it seems as raster scaling.

    This is the code that I used for the test:

    Qt Code:
    1. import QtQuick 2.0
    2. import "../contents"
    3. import "../../shared"
    4.  
    5. Item {
    6. id: container
    7. width: 320
    8. height: 480
    9.  
    10. scale: scaleCtrl2.value
    11.  
    12. onScaleChanged: { canvas.requestPaint() }
    13.  
    14. Column {
    15. spacing: 6
    16. anchors.fill: parent
    17. anchors.topMargin: 12
    18.  
    19. Text {
    20. font.pointSize: 24
    21. font.bold: true
    22. text: "Smile with arcs"
    23. anchors.horizontalCenter: parent.horizontalCenter
    24. color: "#777"
    25. }
    26.  
    27. Canvas {
    28. id: canvas
    29. width: 320 * parent.scale
    30. height: (parent.height - controls.height) * parent.scale
    31. antialiasing: true
    32. renderTarget: Canvas.Image
    33.  
    34. property color strokeStyle: Qt.darker(fillStyle, 1.6)
    35. property color fillStyle: "#e0c31e" // yellow
    36. property int lineWidth: lineWidthCtrl.value
    37. property bool fill: true
    38. property bool stroke: true
    39. property real scale : scaleCtrl.value
    40. property real rotate : rotateCtrl.value
    41.  
    42. onLineWidthChanged:requestPaint();
    43. onFillChanged:requestPaint();
    44. onStrokeChanged:requestPaint();
    45. onScaleChanged:requestPaint();
    46. onRotateChanged:requestPaint();
    47.  
    48. onPaint: {
    49. var ctx = canvas.getContext('2d');
    50. var originX = 85
    51. var originY = 75
    52. ctx.save();
    53. ctx.clearRect(0, 0, canvas.width, canvas.height);
    54. ctx.translate(originX, originX);
    55. ctx.globalAlpha = canvas.alpha;
    56. ctx.strokeStyle = canvas.strokeStyle;
    57. ctx.fillStyle = canvas.fillStyle;
    58. ctx.lineWidth = canvas.lineWidth;
    59.  
    60. ctx.translate(originX, originY)
    61. ctx.scale(canvas.scale, canvas.scale);
    62. ctx.rotate(canvas.rotate);
    63. ctx.translate(-originX, -originY)
    64.  
    65. ctx.beginPath();
    66. ctx.moveTo(75 + 50 * Math.cos(0),
    67. 75 - 50 * Math.sin(Math.PI*2));
    68. ctx.arc(75,75,50,0,Math.PI*2,true); // Outer circle
    69. ctx.moveTo(75,70);
    70. ctx.arc(75,70,35,0,Math.PI,false); // Mouth (clockwise)
    71. ctx.moveTo(60,65);
    72. ctx.arc(60,65,5,0,Math.PI*2,true); // Left eye
    73. ctx.moveTo(90 + 5 * Math.cos(0),
    74. 65 - 5 * Math.sin(Math.PI*2));
    75. ctx.moveTo(90,65);
    76. ctx.arc(90,65,5,0,Math.PI*2,true); // Right eye
    77. ctx.closePath();
    78. if (canvas.fill)
    79. ctx.fill();
    80. if (canvas.stroke)
    81. ctx.stroke();
    82. ctx.restore();
    83. }
    84. }
    85. }
    86. Column {
    87. id: controls
    88. anchors.bottom: parent.bottom
    89. anchors.bottomMargin: 12
    90. Slider {id: lineWidthCtrl ; min: 1 ; max: 10 ; init: 2 ; name: "Outline"}
    91. Slider {id: scaleCtrl ; min: 0.1 ; max: 10 ; init: 1 ; name: "Scale"}
    92. Slider {id: rotateCtrl ; min: 0 ; max: Math.PI*2 ; init: 0 ; name: "Rotate"}
    93. Slider {id: scaleCtrl2 ; min: 0.01 ; max: 2 ; init: 1 ; name: "Parent Scale"}
    94. }
    95. }
    To copy to clipboard, switch view to plain text mode 

    I attach an image of the rendering, that is correct on the left when parent scale is not yet modified, and on the right bad rendering when scane assumes values less or greater than 1:

    rendering.png

Similar Threads

  1. QPen has no effect
    By timmu in forum Qt Programming
    Replies: 1
    Last Post: 3rd January 2013, 12:39
  2. How to achieve Alt+Tab effect ?
    By y.s.bisht in forum Newbie
    Replies: 0
    Last Post: 11th November 2011, 13:22
  3. 3D Effect
    By andzoff in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2010, 11:52
  4. mac os dialog effect?
    By 0xl33t in forum Newbie
    Replies: 5
    Last Post: 3rd August 2009, 17:05
  5. Qwt Knobs
    By eto in forum Qwt
    Replies: 5
    Last Post: 31st December 2008, 10:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.