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. JS loop disables canvas drawing
Forum Updated to NodeBB v4.3 + New Features

JS loop disables canvas drawing

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qtquickcanvasjavascript
3 Posts 2 Posters 2.0k 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.
  • B Offline
    B Offline
    bguivarch
    wrote on 9 May 2016, 08:34 last edited by
    #1

    Hello Qt devs,

    I am facing a very strange issue with the QtQuick Canvas item.
    I am drawing two simple rectangles in a Canvas, but if I put some JS code with a for loop in between the drawing of those rectangle, only the first one is visible.
    Here is my code :

    import QtQuick 2.5
    
    Item {
        width:128;
        height:128;
    
        Canvas
        {
            property int parallels:7; //number of lines for parallels
            
            id:canvas
            width:parent.width;
            height:parent.height;
            onPaint:
            {
                var ctx = canvas.getContext("2d");
                ctx.fillStyle="cyan";
                ctx.fillRect(10,10,50,25);
                if(!m_bCreated)
                {
                    canvas.createTrackball(ctx);
                }
                ctx.fillStyle="magenta";
                ctx.fillRect(10,40,50,25);
            }
    
            function createTrackball(ctx)
            {
                var parallelsLines;
                var mediansLines;
                for( i=1;i<parallels;i++)
                {
                }
                m_bCreated = true;
            }
        }
    }
    

    So as you can see, nothing complicated. But as soon as I uncomment my for loop, the magenta rectangle is not displayed anymore.

    Also, I have noticed that I need to put the Item I am building here in another Item to have the canvas drawn in the Qt Creator Designer. So the code I wrote above is in a file called CameraControlTrackball.qml, and I have created another file called CameraControlTrackballHolder.qml which content is juste the following :

    import QtQuick 2.0
    
    Item {
        width:256;
        height:256;
    
        CameraControlTrackball {
            id: cameraControlTrackball1
            x: 64
            y: 64
        }
    }
    

    So to summerize a little bit, my questions are :
    -why does the for loop disable the display of the magenta rectangle?
    -why does the canvas from CameraControlTrackball.qml cannot be displayed straight from itself in the Designer?

    Thanks for your help!

    P 1 Reply Last reply 9 May 2016, 09:27
    0
    • B bguivarch
      9 May 2016, 08:34

      Hello Qt devs,

      I am facing a very strange issue with the QtQuick Canvas item.
      I am drawing two simple rectangles in a Canvas, but if I put some JS code with a for loop in between the drawing of those rectangle, only the first one is visible.
      Here is my code :

      import QtQuick 2.5
      
      Item {
          width:128;
          height:128;
      
          Canvas
          {
              property int parallels:7; //number of lines for parallels
              
              id:canvas
              width:parent.width;
              height:parent.height;
              onPaint:
              {
                  var ctx = canvas.getContext("2d");
                  ctx.fillStyle="cyan";
                  ctx.fillRect(10,10,50,25);
                  if(!m_bCreated)
                  {
                      canvas.createTrackball(ctx);
                  }
                  ctx.fillStyle="magenta";
                  ctx.fillRect(10,40,50,25);
              }
      
              function createTrackball(ctx)
              {
                  var parallelsLines;
                  var mediansLines;
                  for( i=1;i<parallels;i++)
                  {
                  }
                  m_bCreated = true;
              }
          }
      }
      

      So as you can see, nothing complicated. But as soon as I uncomment my for loop, the magenta rectangle is not displayed anymore.

      Also, I have noticed that I need to put the Item I am building here in another Item to have the canvas drawn in the Qt Creator Designer. So the code I wrote above is in a file called CameraControlTrackball.qml, and I have created another file called CameraControlTrackballHolder.qml which content is juste the following :

      import QtQuick 2.0
      
      Item {
          width:256;
          height:256;
      
          CameraControlTrackball {
              id: cameraControlTrackball1
              x: 64
              y: 64
          }
      }
      

      So to summerize a little bit, my questions are :
      -why does the for loop disable the display of the magenta rectangle?
      -why does the canvas from CameraControlTrackball.qml cannot be displayed straight from itself in the Designer?

      Thanks for your help!

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 9 May 2016, 09:27 last edited by
      #2

      Hi @bguivarch and Welcome,

      I just tried your example and it works. Just a couple of errors in the code that you posted.

      • variable i in the for loop was not declared.
      • m_bCreated was not declared.

      Perhaps you have declared them elsewhere ?

      Is it that thefor loop needs to contain some code to re-create the said problem ?

      157

      B 1 Reply Last reply 9 May 2016, 09:47
      0
      • P p3c0
        9 May 2016, 09:27

        Hi @bguivarch and Welcome,

        I just tried your example and it works. Just a couple of errors in the code that you posted.

        • variable i in the for loop was not declared.
        • m_bCreated was not declared.

        Perhaps you have declared them elsewhere ?

        Is it that thefor loop needs to contain some code to re-create the said problem ?

        B Offline
        B Offline
        bguivarch
        wrote on 9 May 2016, 09:47 last edited by
        #3

        Hello @p3c0 and thanks for your answer.

        Yes the m_bCreated was declared, just a copy/paste issue when creating this post as I didn't paste my full code, just a snippet.
        The issue was in the i variable as you pointed it. I am not very familiar with JS, my bad. Declaring it before the loop solved my problem.

        Thanks for your help!

        1 Reply Last reply
        0

        3/3

        9 May 2016, 09:47

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved