StocQt

可配置 100 种股票的股票图表。

StocQt应用程序以纳斯达克 100 指数为基础,为自定义的 100 种股票列表显示趋势图。用户可从列表中选择股票,并使用QNetworkAccessManager 或离线数据集从金融建模准备 API 获取所需数据。该应用程序还允许收藏最多 5 只股票,以便在比较图表中使用。

该应用程序使用多种自定义类型,如搜索、股票复选框、股票图表、股票视图和设置视图。这些类型以可读形式显示股票数据,并允许用户自定义趋势图。例如,用户可以选择查看每周、每月、每季度或每半年的股价趋势。

该应用程序使用用 C++ 实现的自定义 StockEngine 单例来处理数据,如 API 请求和收藏股票。它实现了一个自定义 APIHandler 类,用于获取在线数据。

voidApiHandler::stockHistory(constQString&symbol,std::function<void(QList<HistoryData>)>onComplete) {if(m_useLiveData) { QDateTimesixMonthsAgo=QDateTime::currentDateTimeUtc().addMonths(-6);        QStringto=QDateTime::currentDateTimeUtc().toString(m_dateFormat);        QStringfrom=sixMonthsAgo.toString(m_dateFormat);        QStringurl=QString("https://financialmodelingprep.com/api/v3/historical-price-full/""%1?from=%2&to=%3&apikey=%4").arg(symbol,from,to,m_apiKey); m_getRequest.setUrl(QUrl(url));        QNetworkReply*reply =m_accessManager.get(m_getRequest);connect(reply, &QNetworkReply::finished, [reply, this,onComplete]() { QStringreplyStr=QString(reply->readAll());如果reply->error()!= QNetworkReply::NoError) {                qDebug() << "Network error" << reply->errorString() << reply->readAll();
            } QByteArraydata=replyStr.toUtf8();            QList<HistoryData>dataList=parseHistory(&data); onComplete(dataList); reply->deleteLater();

应用程序使用ObjectModel 类型访问它所依赖的可视化数据模型。

    id: mainWindow
        ...
    ListView {
        id: root
        ...
        model: ObjectModel {
            Item {
                id: stockContainer
                width: root.width
                height: root.height
                StockView {
                    id: stockView
                    anchors.fill: parent
                    visible: false
                    width: root.width
                    height: root.height
                }

                StockListView {
                    id: listView
                    visible: true
                    anchors.fill: parent
                    width: root.width
                    height: root.height
                }
            }

            FavoriteView {
                id: favoriteView
                width: root.width
                height: root.height
            }

            SettingsView {
                id: infoView
                width: root.width
                height: root.height
            }
        }
    }

StockListModel 是一个自定义数据模型,其中列出了股票的基本信息,如股票 ID、名称、价值和收藏夹。当用户想从列表中选择其他股票时,该应用程序就会使用该数据模型。

StockView 和 FavoriteView 是复杂的数据模型,可显示所选股票或最多 5 只股票的趋势图。它们使用另一种自定义类型--StockChart 或 FavoriteChart,将 StockEngine 中的数据加载到 ListModels 中,并使用Surface3DBars3DScatter3D 来显示股票价格的图形趋势。

Rectangle {
    id: chart
        ...
    function updateHistory(){
        updateStartDate()
        var startPoint = StockEngine.stockModel.indexOf(startDate)
        var totalPoints = StockEngine.stockModel.historyCount()

        var width = startPoint / 50
        for (var i = 0; i < totalPoints; i++) {
            var epochInDays = StockEngine.stockModel.historyDate(i, false) / 86400
            appendSurfacePoint(openModel, width, epochInDays, StockEngine.stockModel.openPrice(i))
            appendSurfacePoint(closeModel,width, epochInDays, StockEngine.stockModel.closePrice(i))
            appendSurfacePoint(highModel,width, epochInDays, StockEngine.stockModel.highPrice(i))
            appendSurfacePoint(lowModel,width, epochInDays, StockEngine.stockModel.lowPrice(i))
        }
        ...
    Surface3D {
        id: historyGraph
        ...
            Surface3DSeries {
                id: highSeries
                visible: true
                baseColor: "green"
                shading: Surface3DSeries.Shading.Flat
                drawMode: Surface3DSeries.DrawSurface
                itemLabelFormat: "Time: @xLabel High:@yLabel$"
                ItemModelSurfaceDataProxy {
                    itemModel: highModel
                    rowRole: "row"
                    columnRole: "column"
                    yPosRole: "value"
                }
            },

设置视图(SettingsView)显示有关应用程序的信息,以及在离线数据和实时数据之间进行选择的下拉菜单。使用实时数据还需要用户提供 API 密钥。个人 API 密钥可从金融建模准备网站生成,然后在选择使用实时数据时将其插入 API 密钥提示中。

Rectangle {
    id: rectangle
        ...
                    function handleKey(keyValid) {
                        if (keyValid) {
                            invalidText.visible = false
                            confirmation.visible = false
                            StockEngine.setUseLiveData(true)
                        }

要更好地了解应用程序,请使用Qt Creator 浏览其代码。

运行示例

运行示例 Qt Creator打开Welcome 模式,然后从Examples 中选择示例。更多信息,请参阅Qt Creator: 教程:构建和运行

示例项目 @ code.qt.io

另请参阅 QML 应用程序

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.