Qt5 GUI QPainter How To Draw Ellipse

In this Qt5 GUI article i want to show you How To Draw Ellipse, we are using QPainter class for Drawing Ellipse, also i will talk about QBrush

and QPen in this article. so first of all let me introduce QPainter class.

 

What is QPainter Class

The QPainter class performs low-level painting on widgets and other paint devices. QPainter provides highly optimized functions to do most of the drawing GUI programs require.

It can draw everything from simple lines to complex shapes like pies and chords. It can also draw aligned text and pixmaps.

Normally, it draws in a “natural” coordinate system, but it can also do view and world transformation. QPainter can operate on any object that inherits the QPaintDevice class.

The common use of QPainter is inside a widget’s paint event: Construct and customize (e.g. set the pen or the brush) the painter. Then draw. Remember to destroy the QPainter

object after drawing.

 

 

What is QBrush

A brush has a style, a color, a gradient and a texture, The brush style() defines the fill pattern using the Qt::BrushStyle enum. The default brush style is Qt::NoBrush (depending on how you construct a brush). This style tells the painter to not fill shapes. The standard style for filling is Qt::SolidPattern. The style can be set when the brush is created using the appropriate constructor, and in addition the setStyle() function provides means for altering the style once the brush is constructed.

Brush Styles
Brush Styles

 

 

What is QPen

A pen has a style(), width(), brush(), capStyle() and joinStyle().

The pen style defines the line type. The brush is used to fill strokes generated with the pen. Use the QBrush class to specify fill styles. The cap style determines the line end caps that can be drawn using QPainter, while the join style describes how joins between two lines are drawn. The pen width can be specified in both integer (width()) and floating point (widthF()) precision. A line width of zero indicates a cosmetic pen. This means that the pen width is always drawn one pixel wide, independent of the transformation set on the painter.

The various settings can easily be modified using the corresponding setStyle(), setWidth(), setBrush(), setCapStyle() and setJoinStyle() functions (note that the painter’s pen must be reset when altering the pen’s properties).

 

 

So first of all you need to create a New Project in Qt5 framework, after that open your mainwindow.h and add this header file.

 

 

Also in your mainwindow.h you need to write the paintEvent() method of QPainter class in the public section of the class

 

 

After adding your mainwindow.h will looks like this

 

 

So after that you need to add the paintEvent() definition in mainwindow.cpp and add the code for drawing Ellipse

 

 

After adding your mainwindow.cpp will be like this

So you can see at the first we have created the QPainter class object, after that we have set the Brush Style for our QPainter.

also we have created a QPen object because we want set color for our Ellipse and at the end we have drawn the ellipse.

 

 

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 

 

 

 

So now run the complete project and this will be the result

Qt5 GUI QPainter How To Draw Ellipse
Qt5 GUI QPainter How To Draw Ellipse

 

 

 

Also you can watch the complete video for this article

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×