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

相关文章

基于Python打造一个可视化FTP服务器

《基于Python打造一个可视化FTP服务器》在日常办公和团队协作中,文件共享是一个不可或缺的需求,所以本文将使用Python+Tkinter+pyftpdlib开发一款可视化FTP服务器,有需要的小... 目录1. 概述2. 功能介绍3. 如何使用4. 代码解析5. 运行效果6.相关源码7. 总结与展望1

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

Qt 中 isHidden 和 isVisible 的区别与使用小结

《Qt中isHidden和isVisible的区别与使用小结》Qt中的isHidden()和isVisible()方法都用于查询组件显示或隐藏状态,然而,它们有很大的区别,了解它们对于正确操... 目录1. 基础概念2. 区别清见3. 实际案例4. 注意事项5. 总结1. 基础概念Qt 中的 isHidd

使用Folium在Python中进行地图可视化的操作指南

《使用Folium在Python中进行地图可视化的操作指南》在数据分析和可视化领域,地图可视化是一项非常重要的技能,它能够帮助我们更直观地理解和展示地理空间数据,Folium是一个基于Python的地... 目录引言一、Folium简介与安装1. Folium简介2. 安装Folium二、基础使用1. 创建

QT移植到RK3568开发板的方法步骤

《QT移植到RK3568开发板的方法步骤》本文主要介绍了QT移植到RK3568开发板的方法步骤,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录前言一、获取SDK1. 安装依赖2. 获取SDK资源包3. SDK工程目录介绍4. 获取补丁包二

Qt把文件夹从A移动到B的实现示例

《Qt把文件夹从A移动到B的实现示例》本文主要介绍了Qt把文件夹从A移动到B的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录如何移动一个文件? 如何移动文件夹(包含里面的全部内容):如何删除文件夹:QT 文件复制,移动(

基于Python开发PDF转PNG的可视化工具

《基于Python开发PDF转PNG的可视化工具》在数字文档处理领域,PDF到图像格式的转换是常见需求,本文介绍如何利用Python的PyMuPDF库和Tkinter框架开发一个带图形界面的PDF转P... 目录一、引言二、功能特性三、技术架构1. 技术栈组成2. 系统架构javascript设计3.效果图

Qt实现发送HTTP请求的示例详解

《Qt实现发送HTTP请求的示例详解》这篇文章主要为大家详细介绍了如何通过Qt实现发送HTTP请求,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、添加network模块2、包含改头文件3、创建网络访问管理器4、创建接口5、创建网络请求对象6、创建一个回复对

Qt 中集成mqtt协议的使用方法

《Qt中集成mqtt协议的使用方法》文章介绍了如何在工程中引入qmqtt库,并通过声明一个单例类来暴露订阅到的主题数据,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一,引入qmqtt 库二,使用一,引入qmqtt 库我是将整个头文件/源文件都添加到了工程中进行编译,这样 跨平台

Python中的可视化设计与UI界面实现

《Python中的可视化设计与UI界面实现》本文介绍了如何使用Python创建用户界面(UI),包括使用Tkinter、PyQt、Kivy等库进行基本窗口、动态图表和动画效果的实现,通过示例代码,展示... 目录从像素到界面:python带你玩转UI设计示例:使用Tkinter创建一个简单的窗口绘图魔法:用