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

相关文章

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要