簡體   English   中英

QML中的ScrollView和Canvas

[英]ScrollView and Canvas in QML

美好的一天! 我正在嘗試使用Canvas在不同的選項卡中顯示多個圖表。 圖形的比例將ScrollView顯示的實際尺寸,因此我想使用ScrollView 在幾個文件中有此代碼:

main.qml

TabView {
    id: tabView
    Layout.alignment: Qt.AlignCenter
    Layout.fillWidth: true
    Layout.fillHeight: true

    Tab1 {
        id: tab1
    }

    //...
}

Tab1.qml

Tab {
    active: true

    function init()
    {
        item.plot.requestPaint()
    }

    ScrollView {
        property var plot: _plot
        Plot {
            width: 3000
            id: _plot
        }
    }
}

Plot.qml

Canvas {
    function draw()
    {
        console.log("draw go")
        var ctx = getContext("2d")
        ctx.reset()

        //...
    }

    onPaint: {
        draw()
    }
}

在某個時候,函數init()被調用。

問題在於,使用ScrollView信號時不會調用Paint 沒有ScrollView一切都會很好。 控制台中不會出現錯誤。

Qt 5.4.1

您需要給Canvas一個高度:

import QtQuick 2.4
import QtQuick.Window 2.0
import QtQuick.Controls 1.2

Window {
    id: window
    visible: true
    width: 300
    height: 300

    ScrollView {
        anchors.fill: parent

        Canvas {
            width: 3000
            height: window.height
            onPaint: {
                console.log("draw go")
            }
        }
    }
}

這對我來說有點困惑,因為文檔說

ScrollView的直接子級只能是一個Item,並且該子級會隱式錨定以填充滾動視圖。

但隨后繼續說:

子項的寬度和高度將用於定義內容區域的大小。

這似乎是矛盾的陳述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM