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. Canvas drawing

Canvas drawing

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

    Hi, I try to draw in a Canvas but it doesn't draw anything :

    Canvas {
                id: mycanvas
                x:0
                y:40
                width:  mainwindows.width
                height: mainwindows.height-40
                ListModel {
                    id: listerect
                }
                ListModel {
                    id: listeind
                }
    onPaint: {
                            var context = mycanvas.getContext("2d");
    			//init L,H                
                            
                            context.clearRect(0, 0, mycanvas.width, mycanvas.height);
    
    
                            context.fillStyle = Qt.rgba(0, 255, 0, 0)
                            context.font = "bold 8px sans-serif";
                    
                                for(var i=0; i < listerect.count; i++){
                                    var ptrect = listerect.get(i)
                                    context.rect(ptrect["x"],ptrect["y"],L/2,H/2)
                                    var ind=listeind.get(i)
                                    context.text(ind,ptrect["x"]+L/2,ptrect["y"]+H/2)
                                 }
    

    I have some listerect.append({"x": i, "y": j)}) in a loop in another function
    has someone a tip ? I call mycanvas.requestPaint() in a further function (when I click on a button)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SergeiLitovko
      wrote on last edited by
      #2

      Hi, you need to use mycanvas.requestPaint() on some events, for example onWidthChanged: mycanvas.requestPaint()

      S 1 Reply Last reply
      0
      • S SergeiLitovko

        Hi, you need to use mycanvas.requestPaint() on some events, for example onWidthChanged: mycanvas.requestPaint()

        S Offline
        S Offline
        stephane78 0
        wrote on last edited by stephane78 0
        #3

        Hi @SergeiLitovko yes but now,I use it (mycanvas.requestPaint())at the end of a function that defines and put the point in the listmodel (that is called in onclicked of a button) and it doesn't draw anything.it should draw my rectangles and my text.so there is something wrong with the canvas.
        it is a program that I have modified to put a canvas,because I wasn't able to delete my window (and the objects )each time I click on the button to create my objects

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Astrinus
          wrote on last edited by
          #4

          You are drawing with fully transparent color (alpha = 0). Furthermore, according to https://doc.qt.io/qt-5/qml-qtqml-qt.html#rgba-method , the values should be 0-1, so you should set context.fillStyle to Qt.rgba(0, 1, 0, 1).

          S 1 Reply Last reply
          1
          • A Astrinus

            You are drawing with fully transparent color (alpha = 0). Furthermore, according to https://doc.qt.io/qt-5/qml-qtqml-qt.html#rgba-method , the values should be 0-1, so you should set context.fillStyle to Qt.rgba(0, 1, 0, 1).

            S Offline
            S Offline
            stephane78 0
            wrote on last edited by
            #5

            @Astrinus thank you, I corrected that and the point ptrec but it doesn't still draw anything

            S 1 Reply Last reply
            0
            • S stephane78 0

              @Astrinus thank you, I corrected that and the point ptrec but it doesn't still draw anything

              S Offline
              S Offline
              stephane78 0
              wrote on last edited by
              #6

              ok it is ```
              context.strokeStyle=
              ....
              context.rect(...
              context.text(

              context.stroke()

              so my problem is solved,I should have used strokeStyle and stroke()
              
              Best regards
              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