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. Not able to call canvas.requestPaint()
Forum Updated to NodeBB v4.3 + New Features

Not able to call canvas.requestPaint()

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlqtquick2qtquickcontrol
3 Posts 2 Posters 2.3k 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.
  • P Offline
    P Offline
    pra7
    wrote on 14 Jul 2017, 10:17 last edited by pra7
    #1

    I am a beginner and I am not getting how to call canvas.requestPaint() in My QMl basically, I need to change the tab:Rectangle based on the tab selected and my Code is below:

    //myTab.qml

    TabView {
    id: tv
    width: parent.width
    height: parent.height
    antialiasing: true

    style: TabViewStyle {
        frameOverlap: -1
    
        tab: Rectangle {              
            color: "Transparent"
            implicitWidth: text1.width + 50
            implicitHeight: 20
            radius: 2
            smooth: true
            Canvas {
                id: canvas1
                anchors.fill: parent
                width: parent.width
                height: parent.height
                onPaint: {
                    styleData.selected ? drawTab(canvas1,"#0C3142") :
                                         drawTab(canvas1,"Transparent") //Some custom JS function to draw a object
                }                 
    
                Text {
                    id: text1
                    height: parent.height
                    verticalAlignment: Text.AlignVCenter
                    anchors.left : parent.left
                    anchors.leftMargin: 15
                    text: styleData.title
                    color: "white"
                }
            }
        }
    
        frame: Rectangle {
            width: parent.width
            height: parent.height
            color: "Transparent"
            border.color:"white"
        }
        tabBar: Rectangle {
            color: "Transparent"
            anchors.fill: parent
        }
    }
    
    Tab {
        id: tab1
        title: "Tab1"
    }
    Tab{
        id: tab2
        title: "Tab2"
    }
    onCurrentIndexChanged: {
        console.log("index changed "+currentIndex)
        canvas1.repaint() //ERRROR - not defind canvas1
    }
    

    }
    i am getting error ReferenceError: canvas1 is not defined when i try to use in onCurrentIndexChanged and i also tried to emit a signal from TabView and made a connection in Canvas{} but even that did'nt solve the issue

    Please Suggest.

    1 Reply Last reply
    0
    • W while1code
      18 Jul 2017, 18:50

      @pra7 said in Not able to call canvas.requestPaint():

      Hi,
      Try to pass your canvas Id as parameter to your function that draws objects.

      Then you get 2d context of your canvas in your function. Like this :

      function draw(canvasId){
      var ctx =canvasId.getContext("2d"); ...
      ...
      }
      peace,
      LA

      P Offline
      P Offline
      pra7
      wrote on 25 Jul 2017, 16:47 last edited by
      #3

      @while1code That's the problem ,i cannot pass canvas ID outside ie.,in onCurrentIndexChanged function.

      Now I achieved the same using signal and slots by adding following code :

      Canvas {
                  id: canvas1
                  anchors.fill: parent
                  width: parent.width
                  height: parent.height
                  onPaint: {
                      styleData.selected ? drawTab(canvas1,"#0C3142") :
                                           drawTab(canvas1,"Transparent") 
                  }  
      
      //*** CONNECT TO SIGNAL HERE ***
      Connections
       {
                  target: tv
                 onRefresh: canvas1.requestPaint() 
       }
      ...
      ...
      ...
      onCurrentIndexChanged: {
          console.log("index changed "+currentIndex)
          refresh() //emiting refresh signal 
      }
      
      1 Reply Last reply
      1
      • W Offline
        W Offline
        while1code
        wrote on 18 Jul 2017, 18:50 last edited by
        #2

        @pra7 said in Not able to call canvas.requestPaint():

        Hi,
        Try to pass your canvas Id as parameter to your function that draws objects.

        Then you get 2d context of your canvas in your function. Like this :

        function draw(canvasId){
        var ctx =canvasId.getContext("2d"); ...
        ...
        }
        peace,
        LA

        P 1 Reply Last reply 25 Jul 2017, 16:47
        0
        • W while1code
          18 Jul 2017, 18:50

          @pra7 said in Not able to call canvas.requestPaint():

          Hi,
          Try to pass your canvas Id as parameter to your function that draws objects.

          Then you get 2d context of your canvas in your function. Like this :

          function draw(canvasId){
          var ctx =canvasId.getContext("2d"); ...
          ...
          }
          peace,
          LA

          P Offline
          P Offline
          pra7
          wrote on 25 Jul 2017, 16:47 last edited by
          #3

          @while1code That's the problem ,i cannot pass canvas ID outside ie.,in onCurrentIndexChanged function.

          Now I achieved the same using signal and slots by adding following code :

          Canvas {
                      id: canvas1
                      anchors.fill: parent
                      width: parent.width
                      height: parent.height
                      onPaint: {
                          styleData.selected ? drawTab(canvas1,"#0C3142") :
                                               drawTab(canvas1,"Transparent") 
                      }  
          
          //*** CONNECT TO SIGNAL HERE ***
          Connections
           {
                      target: tv
                     onRefresh: canvas1.requestPaint() 
           }
          ...
          ...
          ...
          onCurrentIndexChanged: {
              console.log("index changed "+currentIndex)
              refresh() //emiting refresh signal 
          }
          
          1 Reply Last reply
          1

          • Login

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