Qt QChart 图形可视化

2023-12-17 20:58
文章标签 qt 可视化 图形 qchart

本文主要是介绍Qt QChart 图形可视化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!







具体过程就不仔细说了。会在代码中讲解

头文件:

#ifndef YANGSEN_H
#define YANGSEN_H
#include<QWidget>
#include<QGridLayout>
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QDebug>
#include<QPieSeries>
#include<QComboBox>
QT_CHARTS_USE_NAMESPACE
class yangsen:public QWidget
{
    Q_OBJECT
public:
    explicit yangsen(QWidget *parent = 0);
    QGridLayout *grid;
    QChartView *view;
    QChart *show();//显示折线图
    QChart *show1();//显示扇形图
    QChart *show2();//设置曲线图
    QChart *showdcout();//设置圆饼
    QComboBox *ComBox;
    QComboBox *combox1;
    QVector<QChartView*>viewlist;
public slots:
    void change();//设置标签函数
    void change1();//设置主题函数
};
#endif // YANGSEN_H
 

源文件:

#include"yangsen.h"

#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QDebug>
#include<QPieSeries>
#include<QVector>
#include<QSplineSeries>
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QLabel>
QT_CHARTS_USE_NAMESPACE
yangsen::yangsen(QWidget *widget):QWidget(widget)
{
    //初始化下拉框,label
    ComBox=new QComboBox;
    ComBox->addItem("No-legen");
    ComBox->addItem("left");
    ComBox->addItem("right");
    ComBox->addItem("buttom");
    QHBoxLayout *hb=new QHBoxLayout;
    QLabel *label1=new QLabel("Theme:");
    combox1=new QComboBox;
    combox1->addItem("BlueCerulean");
    combox1->addItem("BlueIcy");
    combox1->addItem("BrownSand");
    combox1->addItem("Dark");
    QLabel *label=new QLabel("Legend:");
    hb->addWidget(label1);
    hb->addWidget(combox1);
    hb->addWidget(label);
    hb->addWidget(ComBox);
    //增加间距之间的可延展性。
    hb->addStretch();
    //两个下拉框对应的槽函数
   connect(ComBox,SIGNAL(currentTextChanged(QString)),this,SLOT(change()));
   connect(combox1,SIGNAL(currentTextChanged(QString)),this,SLOT(change1()));
    grid=new QGridLayout;
    //往画布中添加折线图
    view=new QChartView(show());
    viewlist<<view;
    grid->addWidget(view,1,0);
    //往画布中添加扇形图
    view=new QChartView(show1());
    viewlist<<view;
    grid->addWidget(view,1,1);
     //往画布中添加曲线图
    view=new QChartView(show2());
    grid->addWidget(view,2,0);
    viewlist<<view;
    //往画布中添加圆饼图
    view=new QChartView(showdcout());
    grid->addWidget(view,2,1);
    viewlist<<view;
    grid->addLayout(hb,0,0,1,2);
    setLayout(grid);
}
QChart*  yangsen::show()
{
    QLineSeries *series=new QLineSeries;
    *series<<QPointF(0, 6)<<QPointF(2,5)<<QPointF(4,3)<<QPointF(5,5)<<QPointF(8,8)<<QPointF(9,5);
    QChart *ch=new QChart;
    ch->addSeries(series);
    ch->createDefaultAxes();
    ch->legend()->setAlignment(Qt::AlignBottom);
    ch->setTitle("Qt");
    ch->setAnimationOptions(QChart::SeriesAnimations);
   return ch;
}
QChart*  yangsen::show1()
{
    QPieSeries  *pieSeries = new QPieSeries();
    pieSeries->append("Java", 40);
    pieSeries->append("C#", 20);
    pieSeries->append("JS", 10);
    pieSeries->append("C/C++", 30);
    QChart *ch=new QChart;
    ch->addSeries(pieSeries);
    ch->createDefaultAxes();
    ch->setTitle("Qt");
    ch->setAnimationOptions(QChart::SeriesAnimations);
    ch->legend()->setAlignment(Qt::AlignBottom);
    return ch;
}
void yangsen::change()
{
    QChartView *chartView;
        if(ComBox->currentIndex()!=0)
        {
                foreach (chartView, viewlist) {
                    switch(ComBox->currentIndex())
                    {
                    case 1: chartView->chart()->legend()->setAlignment(Qt::AlignTop);break;
                    case 2: chartView->chart()->legend()->setAlignment(Qt::AlignBottom);break;
                    case 3: chartView->chart()->legend()->setAlignment(Qt::AlignLeft);break;
                    case 4: chartView->chart()->legend()->setAlignment(Qt::AlignRight);break;
                    }
                    chartView->chart()->legend()->show();
                }
        }
        else {
            viewlist.at(0)->chart()->legend()->hide();
            viewlist.at(1)->chart()->legend()->hide();
            viewlist.at(2)->chart()->legend()->hide();
            viewlist.at(3)->chart()->legend()->hide();
        }
}
QChart* yangsen::show2()
{
    QSplineSeries *serial=new QSplineSeries;
    *serial<<QPointF(0, 1)<<QPointF(2,2)<<QPointF(3,4)<<QPointF(5,2)<<QPointF(8,1.5)<<QPointF(9,3);
    QChart *ch=new QChart;
    ch->addSeries(serial);
    ch->createDefaultAxes();
    ch->legend()->setAlignment(Qt::AlignBottom);
    ch->setTitle("Qt");
    ch->setAnimationOptions(QChart::SeriesAnimations);
   return ch;
}
QChart* yangsen::showdcout()
{
    QPieSeries *pie=new QPieSeries;
    pie->append("C# 20%", 20);
    QPieSlice *slice =  pie->append("JS 10%", 10);
    slice->setExploded();
    slice->setLabelVisible();
    pie->append("Java 40%", 40);
    pie->setHoleSize(0.4);
    pie->append("C/C++ 30%", 30);
    QChart *ch=new QChart;
    ch->addSeries(pie);
    ch->createDefaultAxes();
    ch->legend()->setAlignment(Qt::AlignBottom);
    ch->setTitle("Qt");
    ch->setAnimationOptions(QChart::SeriesAnimations);
   return ch;
}
void yangsen::change1()
{
    QChartView *view;
    //创建调色板
    QPalette pal = window()->palette();
    foreach(view,viewlist)
    {
        switch(combox1->currentIndex())
        {
        case 0:view->chart()->setTheme(QChart::ChartThemeBlueCerulean);
            pal.setColor(QPalette::Window, QRgb(0x40434a));
            pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));   break;
        case 1:view->chart()->setTheme(QChart::ChartThemeBlueIcy);
            pal.setColor(QPalette::Window, QRgb(0xcee7f0));
            pal.setColor(QPalette::WindowText, QRgb(0x404044));break;
        case 2:view->chart()->setTheme(QChart::ChartThemeBrownSand);
            pal.setColor(QPalette::Window, QRgb(0x9e8965));
            pal.setColor(QPalette::WindowText, QRgb(0x404044));break;
        case 3:view->chart()->setTheme(QChart::ChartThemeDark);
            pal.setColor(QPalette::Window, QRgb(0x121218));
            pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));break;
        }
    }
    //这句挺重要的,没有他,就相当于设置的调色板没有生成、
     window()->setPalette(pal);
}
main.c文件

#include <QtWidgets/QApplication>

#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QDebug>
#include"yangsen.h"
QT_CHARTS_USE_NAMESPACE
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
 
    yangsen *yang=new yangsen();
    QMainWindow window;
    window.setCentralWidget(yang);
    window.setWindowTitle("Miss------");
    window.resize(600, 450);
    window.show();
//![5]
 
    return a.exec();
}


                                    

这篇关于Qt QChart 图形可视化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/505818

相关文章

Qt QCustomPlot库简介(最新推荐)

《QtQCustomPlot库简介(最新推荐)》QCustomPlot是一款基于Qt的高性能C++绘图库,专为二维数据可视化设计,它具有轻量级、实时处理百万级数据和多图层支持等特点,适用于科学计算、... 目录核心特性概览核心组件解析1.绘图核心 (QCustomPlot类)2.数据容器 (QCPDataC

Qt如何实现文本编辑器光标高亮技术

《Qt如何实现文本编辑器光标高亮技术》这篇文章主要为大家详细介绍了Qt如何实现文本编辑器光标高亮技术,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录实现代码函数作用概述代码详解 + 注释使用 QTextEdit 的高亮技术(重点)总结用到的关键技术点应用场景举例示例优化建议

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

Qt之QMessageBox的具体使用

《Qt之QMessageBox的具体使用》本文介绍Qt中QMessageBox类的使用,用于弹出提示、警告、错误等模态对话框,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.简单介绍3.常见函数4.按钮类型(QMessage::StandardButton)5.分步骤实现弹窗6.总结1.引言

Qt中Qfile类的使用

《Qt中Qfile类的使用》很多应用程序都具备操作文件的能力,包括对文件进行写入和读取,创建和删除文件,本文主要介绍了Qt中Qfile类的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录1.引言2.QFile文件操作3.演示示例3.1实验一3.2实验二【演示 QFile 读写二进制文件的过程】4.

使用Vue-ECharts实现数据可视化图表功能

《使用Vue-ECharts实现数据可视化图表功能》在前端开发中,经常会遇到需要展示数据可视化的需求,比如柱状图、折线图、饼图等,这类需求不仅要求我们准确地将数据呈现出来,还需要兼顾美观与交互体验,所... 目录前言为什么选择 vue-ECharts?1. 基于 ECharts,功能强大2. 更符合 Vue