创建K线图
注意:这是 “带小部件的图表”示例库的一部分。

要显示K线图,首先需要创建QCandlestickSeries来处理日数据。此外,我们还指定了自定义的上涨和下跌实体颜色。
auto acmeSeries = new QCandlestickSeries;
acmeSeries->setName("Acme Ltd");
acmeSeries->setIncreasingColor(QColor(Qt::green));
acmeSeries->setDecreasingColor(QColor(Qt::red));QFile 用于访问存储非连续数据的文本文件。CandlestickDataReader 是一个辅助类,用于读取文本文件并从数据中提取开盘价、最高价、最低价、收盘价和时间戳值。关于CandlestickDataReader 的详细说明将在后文中介绍。 方法readCandlestickSet() 读取这些值,并将其设置到该方法返回给调用者的 QCandlestickSet 对象中。返回的 QCandlestickSet 对象会被添加到系列中。我们还保存了自定义类别列表以备后用。
QFile acmeData(":candlestick");
if (!acmeData.open(QIODevice::ReadOnly | QIODevice::Text)) {
m_loadError = QStringLiteral("Failed to load '%1' file.").arg(acmeData.fileName());
return false;
}
QStringList categories;
CandlestickDataReader dataReader(&acmeData);
while (!dataReader.atEnd()) {
QCandlestickSet *set = dataReader.readCandlestickSet();
if (set) {
acmeSeries->append(set);
categories << QDateTime::fromMSecsSinceEpoch(set->timestamp()).toString("dd");
}
}下面,创建了一个新的 QChart 实例,并将之前创建的系列对象添加到其中。我们还定义了一个标题,并将动画设置为QChart::SeriesAnimation 。
auto chart = new QChart;
chart->addSeries(acmeSeries);
chart->setTitle("Acme Ltd. Historical Data (July 2015)");
chart->setAnimationOptions(QChart::SeriesAnimations);在此,我们要求图表为我们的展示创建默认坐标轴。然后,通过从图表中查询坐标轴的指针,并从先前保存的自定义类别列表中设置类别,为横轴设置自定义类别。 我们还通过从图表中查询坐标轴指针,并设置该轴的最小值和最大值,从而设置了纵轴的范围。
chart->createDefaultAxes();
auto axisX = qobject_cast<QBarCategoryAxis *>(chart->axes(Qt::Horizontal).at(0));
axisX->setCategories(categories);
auto axisY = qobject_cast<QValueAxis *>(chart->axes(Qt::Vertical).at(0));
axisY->setMax(axisY->max() * 1.01);
axisY->setMin(axisY->min() * 0.99);下面,我们将图例设为可见,并将其放置在图表底部。
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);最后,我们将图表添加到视图中。
createDefaultChartView(chart);图表现已准备就绪,可以显示了。
下面将详细说明readCandlestickSet() 方法。首先,从文件中读取一行内容,并过滤掉以#开头的行(因为这些被视为注释行)。
QString line = m_textStream.readLine();
if (line.startsWith("#") || line.isEmpty())
return nullptr;文件中的数据以空格分隔的数字序列形式排列。在此代码片段中,该行被拆分为单个数字字符串,并存储在QStringList 中。
QStringList strList = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
if (strList.count() != 5)
return nullptr;要从连续数据中提取值,可使用以下代码。strList 中的值按以下顺序存储:时间戳、开盘价、最高价、最低价、收盘价。
const qreal timestamp = strList.at(0).toDouble();
const qreal open = strList.at(1).toDouble();
const qreal high = strList.at(2).toDouble();
const qreal low = strList.at(3).toDouble();
const qreal close = strList.at(4).toDouble();最后,以下代码片段展示了如何创建一个新的 QCandlestickSet 并为其提供所有必要的值。
auto candlestickSet = new QCandlestickSet(timestamp);
candlestickSet->setOpen(open);
candlestickSet->setHigh(high);
candlestickSet->setLow(low);
candlestickSet->setClose(close);© 2026 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.