【问题标题】:QML - Not able to canvas.requestPaint() from a functionQML - 无法从函数canvas.requestPaint()
【发布时间】:2017-07-14 08:55:51
【问题描述】:

我是初学者,我不知道如何在我的 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 未定义。

请提出建议。

【问题讨论】:

    标签: qt qml qtquick2 qtquickcontrols qtquickcontrols2


    【解决方案1】:

    您的 ID canvas1 在另一个范围内,因为选项卡样式是 Component,因此 ID 对于 TabView 不一定是唯一的。它可能会被实例化多次。

    我对@9​​87654324@ 几乎没有经验,因此可能还有其他解决方案。但是,我会在我触发的TabView 中声明一个信号:refresh,每当我想重新绘制时。

    然后我会使用Connections-元素在Canvas 连接到这个signal 来执行repaint

    例子:

    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
        }
    }
    

    【讨论】:

    • 我添加了一个信号 refresh(),后来在 Canvas 中建立了连接,但我得到了“QML 连接:无法分配给不存在的属性“onRefresh”“错误。
    • 这正是我需要的......我使用了 target:canvas1 这就是它不起作用的原因......谢谢@derM
    猜你喜欢
    • 1970-01-01
    • 2015-08-16
    • 2019-04-18
    • 1970-01-01
    • 2012-03-19
    • 2013-11-28
    • 2015-09-27
    • 2021-04-09
    相关资源
    最近更新 更多