Qt5 Tutorial Creating PieChart with QtChart - Codeloop (2024)

In thisQt5C++ Tutorial i want to show youCreating PieChart with QtChart, basically we are using QtChart class for this article, QtChart module provides a set of easy to use chart components. It uses the Qt Graphics View Framework, therefore charts can be easily integrated to modern user interfaces. Qt Charts can be used as QWidgets,QGraphicsWidget, or QML types. Users can easily create impressive graphs by selecting one of the charts themes, so apie series consists of slices that are defined asQPieSlice objects. The slices can have any values as the QPieSeries object calculates the percentage of a slice compared with the sum of all slices in the series to determine the actual size of the slice in the chart. Pie size and position on the chart are controlled by using relative values that range from 0.0 to 1.0. These relate to the actual chart rectangle. By default, the pie is defined as a full pie. A partial pie can be created by setting a starting angle and angle span for the series. A full pie is 360 degrees, where 0 is at 12 a’clock. also you can check my previous articles on QtChart in the below links.

1:Qt5 C++ Tutorial Creating BarChart

2: Qt5 C++ Tutorial Creating LineChart

OK first of all you need to Create New Project in Qt5. After you have created the new project, open the project file(.pro)and add the charts module to your project, like this:

1

QT += core gui charts

Then, you need to openmainwindow.hand add the following to include the header files that are required for using the charts module:

C++

1

2

3

4

#include<QtCharts>

#include<QChartView>

#include<QPieSeries>

#include<QPieSlice>

The QtCharts and QtChartView headers are both essential for Qt’s charts module. You
must include both of them for any type of chart to work at all. The other headers,
namely QPieSeries and QPieSlice, are used here because we’re going to create a pie chart.
The headers that get included in your project will be different depending on the type of
chart you want to create.

OK now we are going to openmainwindow.ui, and we want to add Horizontal Layout in our gui window.

after that, right-click on the layout widget you just dragged to the central widget, and select
Morph into | QFrame. This will change the layout widget into a QFrame widget while still
maintaining its layout properties. If you create a QFrame from Widget Box, it won’t have
the layout properties that we need. This step is important so that we can set it as the parent
of our chart.

Qt5 Tutorial Creating PieChart with QtChart - Codeloop (1)

So now open yourmainwindow.cppfile and add these codes in the constructor of your class.

C++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

QPieSeries *series = new QPieSeries();

series->append("C++", 80);

series->append("Python", 70);

series->append("Java", 50);

series->append("C#", 40);

series->append("PHP", 30);

QPieSlice *slice = series->slices().at(1);

slice->setExploded(true);

slice->setLabelVisible(true);

slice->setPen(QPen(Qt::darkGreen, 2));

slice->setBrush(Qt::green);

QChart *chart = new QChart();

chart->addSeries(series);

chart->setTitle("Qt5 Pie Chart Example");

QChartView *chartview = new QChartView(chart);

chartview->setParent(ui->horizontalFrame);

After adding your mainwindow.cpp file will look like this .

mainwindow.cpp

C++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

#include "mainwindow.h"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui->setupUi(this);

QPieSeries *series = new QPieSeries();

series->append("C++", 80);

series->append("Python", 70);

series->append("Java", 50);

series->append("C#", 40);

series->append("PHP", 30);

QPieSlice *slice = series->slices().at(1);

slice->setExploded(true);

slice->setLabelVisible(true);

slice->setPen(QPen(Qt::darkGreen, 2));

slice->setBrush(Qt::green);

QChart *chart = new QChart();

chart->addSeries(series);

chart->setTitle("Qt5 Pie Chart Example");

QChartView *chartview = new QChartView(chart);

chartview->setParent(ui->horizontalFrame);

}

MainWindow::~MainWindow()

{

delete ui;

}

So this is the data that we want to add in our piechart, basically we have created the object of QPieSeries and we have added the data to our PieSeries.

C++

1

2

3

4

5

6

7

QPieSeries *series = new QPieSeries();

series->append("C++", 80);

series->append("Python", 70);

series->append("Java", 50);

series->append("C#", 40);

series->append("PHP", 30);

Because we want to slice a part of our PieChart, so for that you need to add these codes.

C++

1

2

3

4

5

QPieSlice *slice = series->slices().at(1);

slice->setExploded(true);

slice->setLabelVisible(true);

slice->setPen(QPen(Qt::darkGreen, 2));

slice->setBrush(Qt::green);

As i have said for creating of charts in Qt5, you need to create QChart with QChartView, so in here first we create QChart and we add our pie series to the QChart object.

C++

1

2

3

QChart *chart = new QChart();

chart->addSeries(series);

chart->setTitle("Qt5 Pie Chart Example");

at the end we need to create a QChartView for rendering our pie chart.

C++

1

2

QChartView *chartview = new QChartView(chart);

chartview->setParent(ui->horizontalFrame);

Also check Qt5 C++ GUI Development Articles in the below links

1:Qt5 C++ Introduction And Installation

2:Qt5 C++ First Console Application

3:Qt5 C++ First GUI Application

4:Qt5 C++ Signal And Slots Introduction

5:Qt5 C++ Layout Management

6:Qt5 C++ Creating Qt Style Sheets

7:Qt5 C++ Creating QPushButton

8:How To Create QCheckBox in Qt5

9:Qt5 GUI How To Create QRadioButton

10:Qt5 GUI Development How To Create ComboBox

11:Qt5 C++ GUI Development Creating QListWidget

12:Qt5 C++ GUI Development Creating QMessageBox

13 :Qt5 C++ GUI Creating QMenu And QToolbar

14:Qt5 C++ GUI Development Creating QPrintDialog

15:Qt5 C++ GUI Development Creating QFontDialog

16:Qt5 C++ GUI Development Creating QColorDialog

17:How to Create QFileDialog in Qt5

18:How to Create QProgressbar in Qt5 C++

19:How to Create QPropertyAnimation in Qt5

20:How To Control QPropertyAnimation in Qt5

21:How To Create AnimationGroup in Qt5

22:How To Create State Machine in Qt5 C++

23:How To Draw Text And Line in Qt5

24:Qt5 GUI How To Draw Rectangle

25:Qt5 GUI How To Draw Ellipse

Run the complete code and this will be the result

Qt5 Tutorial Creating PieChart with QtChart - Codeloop (2)

Also you can watch the complete video for this article

Subscribe and Get Free Video Courses & Articles in your Email

Qt5 Tutorial Creating PieChart with QtChart - Codeloop (2024)
Top Articles
Mysticism | Definition, History, Examples, & Facts
Colleen Camp Breast
Alvin Isd Ixl
Camping World Of New River
Qdoba Calorie Calc
Buff Streams .Io
Busted Newspaper Birmingham Al
Cbs Week 10 Trade Value Chart
Ebony Ts Facials
Teamsideline Manatee
Myvetstoreonline.pharmacy
Weather Channel Quincy
Timothy Warren Cobb Obituary
B Corp: Definition, Advantages, Disadvantages, and Examples
Myworld Interactive American History Pdf
Best Non Toxic Cutting Board for your Healthy Kitchen - Healthy House on the Block
Gcfysl
Cavender’s 50th Anniversary: How the Cavender Family Built — and Continues to Grow — a Western Wear Empire Using Common Sense values
Open jazz : podcast et émission en replay | France Musique
2010 Ford F-350 Super Duty XLT for sale - Wadena, MN - craigslist
Eurail Pass Review: Is It Worth the Price?
Carlitos Caribbean Bar & Grill Photos
Milwaukee Zoo Ebt Discount
Webmail.unt.edu
Restored Republic December 1 2022
Ethos West Mifflin
Highplainsobserverperryton
Sweeterthanolives
Freeway Insurance Actress
What Is a Homily? | Best Bible Commentaries
Weather Tomorrow Hourly At My Location On Netflix Movies
Adventhealth Employee Handbook 2022
Phoenix | Arizona, Population, Map, & Points of Interest
Musc Food Truck Schedule
Point Click Care Cna Login Cna
Dallas College Radiology Packet
Expend4bles | Rotten Tomatoes
Sayre Australian Shepherds
Seattle Rpz
Www.1Tamilmv.cfd
Famous Church Sermons
Family Link from Google - Family Safety & Parental Control Tools
Quazii Plater Nameplates Profile - Quazii UI
Strange World Showtimes Near Amc Marquis 16
Lindy Kendra Scott Obituary
Inter Miami Vs Fc Dallas Total Sportek
How Much Does Costco Gas Cost Today? Snapshot of Prices Across the U.S. | CostContessa
Realidades 2 Capitulo 2B Answers
Epiq Document Delivery
Drew Gulliver Bj
Hurst Scott Funeral Home Obituaries Richlands Virginia
Ravenna Greatsword Arcane Odyssey
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 5745

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.