QSplineSeries相关内容
程序员文章站
2022-05-02 19:27:39
...
学习一下Qt Charts的柱状图部分。参考书籍:Qt5.9 C++开发指南
效果图
头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCharts/QChart>
using namespace QtCharts;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btnSlineColor_clicked();
void on_btnSplinePointColor_clicked();
void on_chkDataPoint_stateChanged(int arg1);
void on_chkDataPointLabelVisible_stateChanged(int arg1);
private:
//创建光滑曲线序列
void createQSplineSeries();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLineSeries>
#include <QSplineSeries>
#include <QValueAxis>
#include <QCategoryAxis>
#include <QColorDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStringList headText = QStringList() << QStringLiteral("星期") << QStringLiteral("最低温度(°C)") << QStringLiteral("最高温度(°C)");
ui->tableWidget->setHorizontalHeaderLabels(headText);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
createQSplineSeries();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createQSplineSeries()
{
ui->chartViewQSpline->setRenderHint(QPainter::Antialiasing);
ui->chartViewQSpline->chart()->setTitle(QStringLiteral("QSplineSeries演示"));
ui->chartViewQSpline->chart()->setTheme(QChart::ChartThemeDark);
//创建光滑曲线序列,并添加数据
QSplineSeries *spLineSeries = new QSplineSeries;
spLineSeries->setName(QStringLiteral("最高温"));
for (int i = 0; i < ui->tableWidget->rowCount(); i++)
{
spLineSeries->append(i, ui->tableWidget->item(i, 2)->text().toInt());
}
ui->chartViewQSpline->chart()->addSeries(spLineSeries);
//创建坐标轴
QValueAxis *axisX = new QValueAxis;
axisX->setRange(0, 6);
axisX->setTickCount(7);
axisX->setLabelsVisible(false);
QValueAxis *axisY = new QValueAxis;
axisY->setRange(0, 20);
ui->chartViewQSpline->chart()->setAxisX(axisX, spLineSeries);
ui->chartViewQSpline->chart()->setAxisY(axisY, spLineSeries);
//创建折现序列,并添加数据
QLineSeries *lineSeries = new QLineSeries;
lineSeries->setName(QStringLiteral("最低温"));
for (int i = 0; i < ui->tableWidget->rowCount(); i++)
{
lineSeries->append(i, ui->tableWidget->item(i, 1)->text().toInt());
}
ui->chartViewQSpline->chart()->addSeries(lineSeries);
ui->chartViewQSpline->chart()->setAxisX(axisX, lineSeries);
ui->chartViewQSpline->chart()->setAxisY(axisY, lineSeries);
//显示曲线的数据点
lineSeries->setPointsVisible(true);
spLineSeries->setPointsVisible(true);
//图例
ui->chartViewQSpline->chart()->legend()->setAlignment(Qt::AlignBottom);
}
void MainWindow::on_btnSlineColor_clicked()
{//设置光滑曲线:线条颜色
QLineSeries *lineSeries = (QLineSeries *)ui->chartViewQSpline->chart()->series().at(0);
QColor color = lineSeries->pen().color();
color = QColorDialog::getColor(color);
QPen pen = lineSeries->pen();
pen.setColor(color);
lineSeries->setPen(pen);
}
void MainWindow::on_btnSplinePointColor_clicked()
{//设置光滑曲线:数据点颜色
QSplineSeries *splineSeries = (QSplineSeries *)ui->chartViewQSpline->chart()->series().at(0);
QColor color = splineSeries->pen().color();
color = QColorDialog::getColor(color);
QPen pen = splineSeries->pen();
pen.setColor(color);
splineSeries->setPen(pen);
}
void MainWindow::on_chkDataPoint_stateChanged(int arg1)
{//设置光滑曲线:数据点显示
QLineSeries *lineSeries = (QLineSeries *)ui->chartViewQSpline->chart()->series().at(0);
QSplineSeries *splineSeries = (QSplineSeries *)ui->chartViewQSpline->chart()->series().at(1);
if (Qt::Checked == arg1)
{
lineSeries->setPointsVisible(true);
splineSeries->setPointsVisible(true);
}
else
{
lineSeries->setPointsVisible(false);
splineSeries->setPointsVisible(false);
}
}
void MainWindow::on_chkDataPointLabelVisible_stateChanged(int arg1)
{//设置光滑曲线:数据点标签显示
QLineSeries *lineSeries = (QLineSeries *)ui->chartViewQSpline->chart()->series().at(0);
QSplineSeries *splineSeries = (QSplineSeries *)ui->chartViewQSpline->chart()->series().at(1);
if (Qt::Checked == arg1)
{
lineSeries->setPointLabelsVisible(true);
splineSeries->setPointLabelsVisible(true);
}
else
{
lineSeries->setPointLabelsVisible(false);
splineSeries->setPointLabelsVisible(false);
}
}