How to Draw Text & Line in Qt5 with QPainter

In this Qt5 article i want to show you How to Draw Text & Line in Qt5 with QPainter , 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.

 

 

 For example:

The core functionality of QPainter is drawing, but the class also provide several functions that allows you to customize QPainter’s settings and its rendering quality, and others that enable clipping. In addition you can control how different shapes are merged together by specifying the painter’s composition mode.

 

 

 

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 texts

 

 

 

 

After adding your mainwindow.cpp will be like this

So in the definition of the method we draw our text and also we have given html styles using QTextDocument

 

 

 

QTextDocument 

QTextDocument is a container for structured rich text documents, providing support for styled text and various types of document elements, such as lists, tables, frames, and images. They can be created for use in a QTextEdit, or used independently.

Each document element is described by an associated format object. Each format object is treated as a unique object by QTextDocuments, and can be passed to objectForFormat() to obtain the document element that it is applied to.

 

 

 

More Qt5 C++ GUI Development Articles 

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 C++

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++

 

 

 

After that run the complete project and this will be the result

How to Draw Text & Line in Qt5 with QPainter
How to Draw Text & Line in Qt5 with QPainter

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×