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. Signals in Qt Quick Controls

Signals in Qt Quick Controls

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

    I am trying to develop a small QML application that consists of a circle that redraws itself using the value from a spinbox as the radius. I have managed to write the initialisation code but I am unable to find the signal that can call requestPaint() on the canvas containing the circle. I have added the code below. If someone could point me in the right direction or provide a comprehensive document about using Qt widgets in QML, that would be great.

    import QtQuick 2.0
    import QtQuick.Controls 1.0
    
    Item {
        id: item
        width: 400
        height: 400
        property var radius: spin.value
        Canvas {
            id: canvas
            width: parent.width
            height: 0.75*parent.height
    
            onPaint: {
                // Get drawing context
                var context = getContext("2d");
    
                // Make canvas all white
                context.beginPath();
                context.clearRect(0, 0, width, height);
                context.fill();
    
                context.beginPath();
                context.fillStyle = "orange"
                context.strokeStyle = "red"
                context.moveTo(width/2+60, height/2);
                context.arc(width/2, height/2, 100, 0, 2*Math.PI, true)
                context.fill();
                context.stroke();
    
                context.translate(width/2,height/2);
    
                for(var i = 0;i<12;i++){
                    context.beginPath();
                    context.lineWidth = 1;
                    context.moveTo(0,0);
                    context.strokeStyle = "yellow";
                    context.lineTo(0,-100);
                    context.stroke();
                    if(i!=12){
                    context.rotate(Math.PI/6);
                    }
                }
    
                context.beginPath();
                context.fillStyle = "white";
                context.strokeStyle = "black";
                context.moveTo(0, 0);
                test();
                context.arc(0, 0, radius, 0, 2*Math.PI, true);
                context.fill();
                context.stroke();
    
    
                //redraw();
                //signal spin.valueChanged();
            }
        }
        Rectangle{
            id: rect;
            color: "white";
            x: 10;
            y: 0.75*parent.height;
            width: parent.width-20;
            height: 0.25*parent.height-20;
            border.color: "black"
    
            SpinBox{
                id: spin
                anchors.centerIn: rect.Center
                maximumValue: 100
                minimumValue: 0
                value: 40
    
            }
        }
        function test(){
            radius = spin.value
            redraw()
            //console.log(spin.data)
            return radius
        }
        function redraw(){
            canvas.requestPaint()
        }
    }
    

    The final design looks like this (https://imgur.com/a/UcJQC)

    The white circle is the one that should redraw itself. Another doubt I have is regarding the spokes. The way I drew it is I drew a single radius line and rotated the canvas. But one of the spokes is shorted that the rest and is visible over the white circle (which it shouldn't).

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Troffe
      wrote on last edited by
      #2

      @cryomick
      Hi, look at this article https://evileg.com/en/post/296/ , maybe it will help something

      1 Reply Last reply
      0
      • PhrogzP Offline
        PhrogzP Offline
        Phrogz
        wrote on last edited by Phrogz
        #3

        How about (untested):

        SpinBox {
            // …
            onValueChanged: canvas.requestPaint()
        }
        

        Or alternatively:

        Component.onCompleted: spin.valueChanged.connect(canvas.requestPaint);
        

        Also, your onPaint code starts off a little weird:

        // Make canvas all white
        context.beginPath();
        context.clearRect(0, 0, width, height);
        context.fill();
        

        I'm pretty sure 2 of those 3 code lines are unnecessary.

        1. There's no reason to begin a path before calling clearRect().
        2. You don't need to fill() after calling clearRect(). Indeed, since you created a new path right before calling fill(), it will have no effect.
        1 Reply Last reply
        1
        • C Offline
          C Offline
          cryomick
          wrote on last edited by
          #4

          Thank you!! This was what I was looking for!!!!

          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