Qt QML-无法从函数中画布.requestPaint()

Qt QML-无法从函数中画布.requestPaint(),qt,qml,qtquick2,qtquickcontrols,qtquickcontrols2,Qt,Qml,Qtquick2,Qtquickcontrols,Qtquickcontrols2,我是初学者,不知道如何在QML中调用canvas.requestPaint(),代码如下: //myTab.qml TabView { id: tv width: parent.width height: parent.height antialiasing: true style: TabViewStyle { frameOverlap: -1 tab: Rectangle {

我是初学者,不知道如何在QML中调用canvas.requestPaint(),代码如下:

//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
    }
}
当我尝试在
onCurrentIndexChanged
中使用时,出现以下错误:

ReferenceError:未定义canvas1


请建议。

您在另一个范围内有id
canvas1
,因为选项卡样式是
组件
,因此id对于
选项卡视图不一定是唯一的。它可能被实例化多次

我对
选项卡视图
几乎没有经验,因此可能还有另一种解决方案。然而,我会在
选项卡中声明一个信号:
刷新
,每当我想重新绘制时,我都会触发该信号

然后我将使用
画布中的
连接
-元素连接到该
信号
以执行
重新绘制

例如:

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

    signal refresh // *** DEFINE SIGNAL HERE

    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
                }

                function drawTab() { // *** I DONT KNOW WHAT SHOULD BE DONE HERE
                    console.log('do nothing')
                }

                // *** CONNECT TO SIGNAL HERE ***
                Connections {
                    target: tv
                    onRefresh: canvas1.requestPaint() // *** repaint is not a function.
                }

                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)
        refresh() // *** INVOKE SIGNAL HERE
    }
}

我添加了一个signal refresh(),后来在画布中建立了一个连接,但我得到了“QML Connections:无法分配给不存在的属性”onRefresh“”错误。这正是我需要的。。我使用了target:canvas1,这就是它不起作用的原因……谢谢@derM