欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

QT使用QChart繪制餅圖

 更新時(shí)間:2024年11月19日 11:26:00   作者:姆路  
在Qt中使用QChart類可以快速繪制一個(gè)圖表出來,比如折線圖、餅圖、柱狀圖等,本文就來為大家介紹一下如何利用QChart繪制簡單的餅圖吧

餅圖沒有坐標(biāo)軸,也不需要坐標(biāo)軸

使用的系列是QPieSeries

餅圖里面一個(gè)一個(gè)的塊稱為切片

舉例:

繪制一個(gè)餅圖,點(diǎn)擊對應(yīng)切片可以讓切片分離

1、創(chuàng)建圖表視圖并開啟抗鋸齒

//1、創(chuàng)建圖表視圖
QChartView * view = new QChartView(this);
//開啟抗鋸齒
view -> setRenderHint(QPainter::Antialiasing);

2、創(chuàng)建圖表并將圖表設(shè)置給圖表視圖

//2.創(chuàng)建圖表
QChart * chart = new QChart();
 
//3.將圖表設(shè)置給視圖
view -> setChart(chart);
 
//設(shè)置標(biāo)題
chart -> setTitle("餅圖");
chart -> legend() -> show(); //顯示或者隱藏圖例(默認(rèn)顯示)

3、創(chuàng)建餅圖系列,并給系列添加切片,并設(shè)置切片的相關(guān)屬性,并綁定餅圖系列的信號

//創(chuàng)建系列,餅圖沒有坐標(biāo)軸
QPieSeries * pie = new QPieSeries(this);
//添加切片
//參數(shù)1:對應(yīng)的圖例名和標(biāo)簽名(標(biāo)簽?zāi)J(rèn)不顯示)
//參數(shù)2:占據(jù)的數(shù)值
QPieSlice * ps1 = new QPieSlice("C語言", 40, this);
pie -> append(ps1);
QPieSlice * ps2 = new QPieSlice("C++", 30, this);
pie -> append(ps2);
 
//也可以直接在append中指定
pie -> append("JAVA", 25);
pie -> append("C#", 5);
 
//設(shè)置中間圓孔的大小,值不能超過1
pie -> setHoleSize(0.2);
 
//獲取餅圖的切片
//并設(shè)置切片的屬性
auto p1 = pie -> slices().at(0);
 
//獲取切片占的百分比,是一個(gè)小數(shù)
auto per = p1 -> percentage();
 
//設(shè)置切片的標(biāo)簽提示
p1 -> setLabel("c語言占" + QString::number(per * 100) + "%");
p1 -> setLabelVisible(true);
 
//p1->setPen()//設(shè)置畫筆,改變輪廓
p1 -> setBrush(Qt::red); //改變填充
 
//點(diǎn)擊信號,某個(gè)切片被點(diǎn)擊了就會(huì)發(fā)出信號,信號參數(shù)告訴了是哪一個(gè)切片被點(diǎn)擊了
connect(pie, & QPieSeries::clicked, this, [ = ](QPieSlice * p) {
    //將被點(diǎn)擊的切片和餅圖分離
    if (p -> isExploded()) {
        p -> setExploded(false);
    } else {
        p -> setExploded(true);
    }
 
});

4、將系列添加到圖表中

//添加系列
chart->addSeries(pie);

完整代碼:

#ifndef WIDGET_H
#define WIDGET_H
 
#include <QWidget>
#include<QtCharts>
#include<QHBoxLayout>
#include<QPieSlice>
#include<QPieSeries>
#include<QBarSeries>
#include<QBarSet>
#include<QAreaSeries>
 
class Widget : public QWidget
{
    Q_OBJECT
 
public:
    Widget(QWidget *parent = nullptr) : QWidget(parent)
    {
        resize(800,600);
        QHBoxLayout* h_box=new QHBoxLayout(this);
        
        drawPiePic();
 
    }
    ~Widget()=default;
 
    //畫餅圖
    void drawPiePic()
    {
        //1、創(chuàng)建圖表視圖
        QChartView* view=new QChartView(this);
        //開啟抗鋸齒
        view->setRenderHint(QPainter::Antialiasing);
 
        this->layout()->addWidget(view);
 
        //2.創(chuàng)建圖表
        QChart* chart=new QChart();
 
        //3.將圖表設(shè)置給視圖
        view->setChart(chart);
 
 
        //設(shè)置標(biāo)題
        chart->setTitle("餅圖");
        chart->legend()->show();//顯示或者隱藏圖例(默認(rèn)顯示)
 
        //創(chuàng)建系列,餅圖沒有坐標(biāo)軸
        QPieSeries* pie=new QPieSeries(this);
        //添加切片
        //參數(shù)1:對應(yīng)的圖例名和標(biāo)簽名(標(biāo)簽?zāi)J(rèn)不顯示)
        //參數(shù)2:占據(jù)的數(shù)值
        QPieSlice* ps1=new QPieSlice("C語言",40,this);
        pie->append(ps1);
        QPieSlice* ps2=new QPieSlice("C++",30,this);
        pie->append(ps2);
 
        //也可以直接在append中指定
        pie->append("JAVA",25);
        pie->append("C#",5);
 
        //設(shè)置中間圓孔的大小,值不能超過1
        pie->setHoleSize(0.2);
 
        //獲取餅圖的切片
        //并設(shè)置切片的屬性
        auto p1=pie->slices().at(0);
 
        //獲取切片占的百分比,是一個(gè)小數(shù)
        auto per=p1->percentage();
 
        //設(shè)置切片的標(biāo)簽提示
        p1->setLabel("c語言占"+QString::number(per*100)+"%");
        p1->setLabelVisible(true);
 
        //p1->setPen()//設(shè)置畫筆,改變輪廓
        p1->setBrush(Qt::red);//改變填充
 
        //點(diǎn)擊信號,某個(gè)切片被點(diǎn)擊了就會(huì)發(fā)出信號,信號參數(shù)告訴了是哪一個(gè)切片被點(diǎn)擊了
        connect(pie,&QPieSeries::clicked,this,[=](QPieSlice* p){
            //將被點(diǎn)擊的切片和餅圖分離
            if(p->isExploded())
            {
                p->setExploded(false);
            }
            else
            {
                p->setExploded(true);
            }
 
        });
 
        //添加系列
        chart->addSeries(pie);
 
    }
 
 
};
#endif // WIDGET_H

到此這篇關(guān)于QT使用QChart繪制餅圖的文章就介紹到這了,更多相關(guān)QT QChart繪制餅圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論