How to Draw a Line in C++ Console

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:

void SimpleExampleWidget :: paintEvent ( QPaintEvent * )

{

QPainter painter ( this ) ;

painter . setPen ( Qt :: blue ) ;

painter . setFont ( QFont ( "Arial" , 30 ) ) ;

painter . drawText ( rect ( ) , Qt :: AlignCenter , "Qt" ) ;

}

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.hand add this header file

#include<QPainter>

#include<QTextDocument>

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

virtual void paintEvent ( QPaintEvent * event ) ;

After adding your mainwindow.h will looks like this

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

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include <QMainWindow>

#include<QPainter>

#include<QTextDocument>

namespace Ui {

class MainWindow ;

}

class MainWindow : public QMainWindow

{

Q_OBJECT

public :

explicit MainWindow ( QWidget * parent = 0 ) ;

~ MainWindow ( ) ;

virtual void paintEvent ( QPaintEvent * event ) ;

private :

Ui :: MainWindow * ui ;

} ;

0 Response to "How to Draw a Line in C++ Console"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel