Qt Quick 示例 - StocQt股票趨勢圖開發

Qt quick演示-StocQtcanvas

Qt是一個跨平臺框架,一般用做圖形工具包,它不只建立CLI應用程序中很是有用。並且它也能夠在三種主要的臺式機操做系統以及移動操做系統(如Symbian,Nokia Belle,Meego Harmattan,MeeGo或BB10)以及嵌入式設備,Android(Necessitas)和iOS的端口上運行。如今咱們爲你提供了免費的試用版。框架

下載Qt6最新試用版工具

Qt組件推薦:ui

  • QtitanRibbon下載試用: 遵循Microsoft Ribbon UI Paradigm for Qt技術的Ribbon UI組件,致力於爲Windows、Linux和Mac OS X提供功能完整的Ribbon組件。
  • QtitanChart | 下載試用 :是一個C ++庫,表明一組控件,這些控件使您能夠快速地爲應用程序提供漂亮而豐富的圖表。而且支持全部主要的桌面

NASDAQ-100的可配置庫存圖表。url

StocQt應用呈現納斯達克100股票列表的第一個股票的走勢圖。它容許用戶從列表中選擇另外一種股票,並使用從離線數據集中獲取所需數據XMLHttpRequest。操作系統

該應用程序使用幾種自定義類型,例如Button,CheckBox,StockChart,StockInfo,StockView等。這些類型用於以可讀形式顯示庫存數據,還可讓用戶自定義趨勢圖。例如,用戶能夠選擇查看股票價格的每週,每個月,每季度或半年趨勢。.net

該應用程序使用ObjectModel類型來訪問它依賴的兩個可視數據模型。code

ListView {
            id: root
        ...
            model: ObjectModel {
                StockListView {
                    id: listView
                    width: root.width
                    height: root.height
                }

                StockView {
                    id: stockView
                    width: root.width
                    height: root.height
                    stocklist: listView
                    stock: stock
                }
            }
        }

StockListView模型是一個靜態數據模型,該模型列出了NASDAQ-100股票,其中包含基本信息,例如stockId,名稱,價值,更改等。若是用戶要從列表中選擇另外一隻股票,則應用程序將使用此數據模型。生命週期

StockView是一個複雜的數據模型,可顯示所選股票的趨勢圖。它使用另外一種自定義類型StockChart,它使用Canvas呈現股票價格的圖形趨勢。在應用程序的生命週期中,大部分時間都使用此數據模型。ip

Rectangle {
    id: chart
    ...
        Canvas {
            id: canvas
        ...
            onPaint: {
                numPoints = stockModel.indexOf(chart.startDate);
                if (chart.gridSize == 0)
                    chart.gridSize = numPoints

                var ctx = canvas.getContext("2d");
                ctx.globalCompositeOperation = "source-over";
                ctx.lineWidth = 1;

                drawBackground(ctx);

                if (!stockModel.ready) {
                    drawError(ctx, "No data available.");
                    return;
                }

                var highestPrice = 0;
                var highestVolume = 0;
                var lowestPrice = -1;
                var points = [];
                for (var i = numPoints, j = 0; i >= 0 ; i -= pixelSkip, j += pixelSkip) {
                    var price = stockModel.get(i);
                    if (parseFloat(highestPrice) < parseFloat(price.high))
                        highestPrice = price.high;
                    if (parseInt(highestVolume, 10) < parseInt(price.volume, 10))
                        highestVolume = price.volume;
                    if (lowestPrice < 0 || parseFloat(lowestPrice) > parseFloat(price.low))
                        lowestPrice = price.low;
                    points.push({
                                    x: j * xGridStep,
                                    open: price.open,
                                    close: price.close,
                                    high: price.high,
                                    low: price.low,
                                    volume: price.volume
                                });
                }

                if (settings.drawHighPrice)
                    drawPrice(ctx, 0, numPoints, settings.highColor, "high", points, highestPrice, lowestPrice);
                if (settings.drawLowPrice)
                    drawPrice(ctx, 0, numPoints, settings.lowColor, "low", points, highestPrice, lowestPrice);
                if (settings.drawOpenPrice)
                    drawPrice(ctx, 0, numPoints,settings.openColor, "open", points, highestPrice, lowestPrice);
                if (settings.drawClosePrice)
                    drawPrice(ctx, 0, numPoints, settings.closeColor, "close", points, highestPrice, lowestPrice);

                drawVolume(ctx, 0, numPoints, settings.volumeColor, "volume", points, highestVolume);
                drawScales(ctx, highestPrice, lowestPrice, highestVolume);
            }
        }

        Text {
            id: fromDate
            color: "#000000"
            font.family: Settings.fontFamily
            font.pointSize: 8
            Layout.alignment: Qt.AlignLeft
            text: "| " + startDate.toDateString()
        }
        Text {
            id: toDate
            color: "#000000"
            font.family: Settings.fontFamily
            font.pointSize: 8
            Layout.alignment: Qt.AlignRight
            Layout.rightMargin: canvas.tickMargin
            Layout.columnSpan: 5
            text: endDate.toDateString() + " |"
        }
    }
}

爲了更好地瞭解應用程序,請使用Qt Creator瀏覽其代碼。