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. Repeater and Delegate: ctx.fillStyle does not update color on DataChanged
Qt 6.11 is out! See what's new in the release blog

Repeater and Delegate: ctx.fillStyle does not update color on DataChanged

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 184 Views
  • 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.
  • I Offline
    I Offline
    Igor23
    wrote last edited by
    #1

    Hallo,

    in my QML file I have the following Repeater. When I emit onDataChanged in my QAbstractListModel the coordinates of the circles are updated and the circles are drawn on the new place. But, I also change the colors of the circles in the model and these colors are not updated (ctx.fillStyle = tcolor). They stay in their initial value:
    (there are also other geometric objects, but that is not important for that)

    Repeater {
      id: partilcesreapeter
      model: Entrymodel
      delegate: chooser
      DelegateChooser{
        id: chooser
        role: "type"
        DelegateChoice{
          roleValue: "sphere";
          delegate: Item{
             id: sphereimage
             required property string tcolor
             required property int iparticle
             required property int semimajoraxis
             required property int semiminoraxis
             required property int xcoord
             required property int ycoord
             required property int angle
             width: 2.0*Number(semimajoraxis)
             height: 2.0*Number(semiminoraxis)
             x: Number(xcoord - semimajoraxis)
             y: Number(ycoord - semiminoraxis)
             Canvas {
                anchors.fill: parent                        
                onPaint: {
                   var ctx = getContext('2d');
                   ctx.beginPath();
                   ctx.lineWidth = 1
                   ctx.strokeStyle = "black"
                   ctx.fillStyle = tcolor
                   ctx.ellipse(x,y,width,height)
                   ctx.stroke();
                   ctx.fill();
                   }
               }
               rotation: Number(angle)
               DelegateChoice{
                  [...]
               }
            }
         }
      }
    }
    

    So, what can I do?

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote last edited by
      #2

      The Canvas is updated/repainter because its size changes but it doesn't know it should depend on tcolor.
      In your delegate, do onTcolorChanged: canvas.requestPaint().

      It won't change much here but is there a reason for not directly using Canvas as your delegate here?

      I 1 Reply Last reply
      2
      • GrecKoG GrecKo

        The Canvas is updated/repainter because its size changes but it doesn't know it should depend on tcolor.
        In your delegate, do onTcolorChanged: canvas.requestPaint().

        It won't change much here but is there a reason for not directly using Canvas as your delegate here?

        I Offline
        I Offline
        Igor23
        wrote last edited by
        #3

        @GrecKo Thanks a lot!
        in Canvas I wrote

        id: canvasSphere
        

        and before in delegate: Item{

        onTcolorChanged: canvasSphere.requestPaint()
        

        Now the colors are updated.

        I don't use Canvas directly, because there are different geometric objects, which I choose by

        role: "type"
           DelegateChoice{
                roleValue: "sphere";
        

        Or is there a simplier way to do this?

        GrecKoG 1 Reply Last reply
        0
        • I Igor23

          @GrecKo Thanks a lot!
          in Canvas I wrote

          id: canvasSphere
          

          and before in delegate: Item{

          onTcolorChanged: canvasSphere.requestPaint()
          

          Now the colors are updated.

          I don't use Canvas directly, because there are different geometric objects, which I choose by

          role: "type"
             DelegateChoice{
                  roleValue: "sphere";
          

          Or is there a simplier way to do this?

          GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote last edited by
          #4

          @Igor23 said in Repeater and Delegate: ctx.fillStyle does not update color on DataChanged:

          I don't use Canvas directly, because there are different geometric objects [...]

          My question was about the delegate inside the DelegatedChoice.

          Instead of DelegateChoice { delegate: Item { Canvas {} } you could do DelegateChoice { delegate: Canvas {}} if your Item doesn't hold more stuff.

          I 1 Reply Last reply
          0
          • GrecKoG GrecKo

            @Igor23 said in Repeater and Delegate: ctx.fillStyle does not update color on DataChanged:

            I don't use Canvas directly, because there are different geometric objects [...]

            My question was about the delegate inside the DelegatedChoice.

            Instead of DelegateChoice { delegate: Item { Canvas {} } you could do DelegateChoice { delegate: Canvas {}} if your Item doesn't hold more stuff.

            I Offline
            I Offline
            Igor23
            wrote last edited by
            #5

            @GrecKo I also added some text to the circle.

            1 Reply Last reply
            0
            • I Igor23 has marked this topic as solved

            • Login

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