《Qt5+QtChart绘制折线图(QLineSeries类)》

QtChart可用于绘制折线图,在折线图中,数据点通过直线连接。要想绘制折线图至少要用到QLineSeries、QChart、QChartView这三个类。QLineSeries类用于创建有由一系列点组成的折线;QChart类用于控制整个图表界面的设置,例如坐标轴、主题、背景色、标题、legend等;QChartView类用于显示图表,将图表显示到QWidget或者QGraphicsWidget上。

 

 

代码示例


打开Qt,新建一个Qt Widgets Application项目,使用QtChart绘制图表需要在pro文件中添加QT += charts,并且还需要在编写代码文件中添加名称空间QT_CHARTS_USE_NAMESPACE,一般放在头文件的后面,Pro文件如下所示:

#-------------------------------------------------
#
# Project created by QtCreator 2018-12-10T12:52:02
#
#-------------------------------------------------
QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QT += charts
TARGET = LineChart
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \       
 main.cpp \       
 mainwindow.cpp
HEADERS += \        
mainwindow.h
FORMS += \        
mainwindow.ui

然后在main.cpp文件中添加如下代码,代码已经注释的很详细了:

#include "mainwindow.h"
#include <QApplication>
#include <QtCharts/QChartView>//显示图表
#include <QtCharts/QLineSeries>//线系列
#include <QDoubleSpinBox>

QT_CHARTS_USE_NAMESPACE//QtChart名空间

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    QList<QLineSeries *> m_series;
    QLineSeries *series1 = new QLineSeries();//实例化一个QLineSeries对象
    QLineSeries *series2 = new QLineSeries();//实例化一个QLineSeries对象
    m_series.append(series1);
    m_series.append(series2);
    //设置线条名称
    series1->setName(QString("line " + QString::number(1)));
    series2->setName(QString("line " + QString::number(2)));
    //series->setName(QString("line " + QString::number(m_series.count())));
    //设置线条颜色,如果不设置会给默认颜色
    series1->setColor(QColor(255,0,255));
    series2->setColor(QColor(0,255,255));
    //series1->setBrush(QColor(255,0,255));
    //series1->setPen(QColor(255,0,255));
    //设置是否线条可视
    series1->setVisible(true);
    series2->setVisible(true);
    //点标签是否可视
    series1->setPointLabelsVisible(true);
    series2->setPointLabelsVisible(true);
    //点标签颜色
    series1->setPointLabelsColor(QColor(255,255,255));
    series2->setPointLabelsColor(QColor(255,255,255));
    //点标签字体
    series1->setPointLabelsFont(QFont("微软雅黑"));
    series2->setPointLabelsFont(QFont("微软雅黑"));
    //设置点标签显示格式
    series1->setPointLabelsFormat("(@xPoint,@yPoint)");
    series2->setPointLabelsFormat("(@xPoint,@yPoint)");
    //是否切割边缘点标签,默认为true
    series1->setPointLabelsClipping(false);
    series2->setPointLabelsClipping(true);
    //设置点标签是否可视
    series1->setPointsVisible(true);
    series2->setPointsVisible(true);

    //添加坐标点
    series1->append(0, 6);
    series1->append(2, 4);
    series1->append(3, 8);
    series1->append(7, 4);
    series1->append(10, 5);
    *series1 << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);

    series2->append(0, 8);
    series2->append(2, 5);
    series2->append(3, 3);
    series2->append(7, 9);
    series2->append(10, 2);
    *series2 << QPointF(11, 3) << QPointF(13, 8) << QPointF(17, 2) << QPointF(18, 5) << QPointF(20, 7);


    QChart *chart = new QChart();
    chart->setTheme(QChart::ChartThemeBlueCerulean);//设置系统主题
    chart->setAnimationOptions(QChart::AllAnimations);//设置启用或禁用动画
    //chart->setBackgroundBrush(QBrush(QColor(170,170,255)));//设置背景色,主题和背景二选一
    //chart->setDropShadowEnabled(true);//是否背景阴影
    chart->setLocalizeNumbers(true);//数字是否本地化
    //chart->legend()->show();//legend是否显示,show和hide
    chart->addSeries(series1);//添加系列到QChart上
    chart->addSeries(series2);//添加系列到QChart上
    chart->createDefaultAxes();//创建默认轴
    chart->setTitle("Simple line chart example");//设置标题
    chart->setTitleBrush(QBrush(QColor(255,170,255)));//设置标题Brush
    chart->setTitleFont(QFont("微软雅黑"));//设置标题字体

    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignBottom);//底部对齐
    //chart->legend()->detachFromChart();
    chart->legend()->setBackgroundVisible(true);//设置背景是否可视
    //chart->legend()->setAutoFillBackground(true);//设置背景自动填充
    //chart->legend()->setColor(QColor(255,128,128,128));//设置颜色
    //chart->legend()->setContentsMargins(10,10,10,10);//设置边距left,top,right,bottom
    chart->legend()->setLabelColor(QColor(255,128,255));//设置标签颜色
    chart->legend()->setVisible(true);//设置是否可视
    //chart->legend()->setMaximumHeight(50);
    //chart->legend()->setMaximumWidth(120);
    //chart->legend()->setMaximumSize(10000);
    //chart->legend()->setGeometry(50,50,120,50);//设置几何位置x,y,w,h
    //chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
    //chart->legend()->setPen(QPen(QColor(192,192, 192,192)));
    chart->legend()->setBorderColor(QColor(255,255,170,185));//设置边框颜色
    QFont font = chart->legend()->font();
    font.setItalic(!font.italic());
    chart->legend()->setFont(font);//设置字体为斜体
    font.setPointSizeF(12);
    chart->legend()->setFont(font);//设置字体大小
    chart->legend()->setFont(QFont("微软雅黑"));//设置字体类型


    QChartView *chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);

    w.setCentralWidget(chartView);
    w.resize(960, 720);
    w.show();

    return a.exec();
}

 

完整代码


优快云下载:https://download.youkuaiyun.com/download/mars_xiaolei/10840847

百度网盘链接:https://pan.baidu.com/s/1sYzagekYPpEz41cHRfctQg
提取码:ptui 

 

<think>好的,用户的问题是如何在QtChart中设置或绘制虚线。我需要先回想一下相关的QtChart知识,特别是关于QLineSeries的样式设置。 首先,我记得QLineSeries是用来绘制折线图的,可能可以通过设置其属性来改变线条样式。在Qt中,通常使用QPen来定义线条的样式,包括颜色、宽度和虚线模式。所以,应该用QPen来设置QLineSeries的线条。 接下来,我需要确认具体的步骤。用户想用虚线,所以需要创建一个QPen对象,设置它的样式为Qt::DashLine或者其他虚线样式,然后应用到QLineSeries上。同时,可能还需要考虑如何调整虚线的参数,比如虚线的间隔和线段长度。 然后,查看提供的引用内容。引用[1]提到QLineSeries用于创建由点组成的折线,这说明操作QLineSeries是正确的方向。引用中没有直接提到虚线设置,但结合Qt的一般做法,使用QPen是合理的。 还需要注意,可能需要在代码中正确引入相关的头文件,比如QPen和Qt命名空间。另外,用户可能在使用QML还是Widgets模块,但根据引用中的示例,用户可能是在使用C++和Widgets,所以回答应基于C++的代码。 另外,引用[2]提到plotArea的设置可能需要调整尺寸,这可能与显示效果有关,但用户的问题是关于线条样式,所以暂时可以忽略这部分,除非设置虚线后显示有问题,可能需要检查绘图区域的大小。 最后,需要给出示例代码,展示如何创建QPen并应用到QLineSeries。同时,可能提到其他可用的虚线样式,如Qt::DashDotLine等,以及如何自定义虚线模式。</think>在QtChart中设置虚线效果需要通过QLineSeries的画笔属性进行样式定制,具体实现步骤如下: 1. 创建QLineSeries对象 2. 创建QPen定义虚线样式 3. 设置画笔到序列对象 4. 将序列添加到图表 ```cpp // 创建折线序列 QLineSeries *series = new QLineSeries(); // 配置虚线画笔 QPen pen(Qt::blue); // 设置颜色 pen.setWidth(2); // 线宽 pen.setStyle(Qt::DashLine); // 设置虚线样式 series->setPen(pen); // 添加数据点 series->append(0, 2); series->append(2, 5); // 将序列添加到图表 QChart *chart = new QChart(); chart->addSeries(series); ``` 支持以下预定义虚线样式: - Qt::SolidLine(实线,默认) - Qt::DashLine(虚线) - Qt::DotLine(点线) - Qt::DashDotLine(点划线) - Qt::CustomDashLine(自定义模式) 如需自定义虚线模式,可以使用`setDashPattern()`方法: ```cpp QPen customPen(Qt::red); customPen.setDashPattern({5, 3, 2, 3}); // 交替显示5单位线段和3单位间隔 series->setPen(customPen); ``` 注意:当使用QML实现时,需要设置ChartView的足够高度才能正确显示虚线样式(推荐高度≥86像素)[^2]
评论 13
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值