Qt – Basic Graphics Drawing

1. Basic graphics drawing

A. The key role in the Qt graphics system
QPainter–The painter in Qt can draw various foundations Graphics, with brushes, brushes, and fonts needed for drawing
QPaintDevice–Canvas in Qt, painter’s drawing board, all QWidget classes inherit from QPaintDevice
The key role in the Qt graphics system
Qt--Basic graphics drawing< br>Tools used in Qt
1.QPen-Used to draw the edges of geometric figures, composed of parameters such as color, width, line style, etc.
2. QBrush-a palette used to fill geometric figures, composed of colors and filling styles
3.QFont-used for text drawing, composed of font attributes
Qt--Basic graphics drawing
Note that-only in Drawing graphics in QWidget::paintEvent
Qt--Basic graphics drawing
B. Coordinate system in Qt graphics system
1. Physical coordinate system-the position of the origin (0,0) in the upper left corner, the unit is pixels and the X coordinate increases to the right, and the Y coordinate goes down Growth
2. Logical coordinate system–the abstract coordinate system in the mathematical model. The unit is determined by the specific problem, and the growth direction of the coordinate axis is determined by the specific problem.
What we need to pay attention to is that QPainter uses the logical coordinate system to draw graphics. The size and position of the graphics in the logical coordinate system are converted and drawn on the specific device, and the logical coordinates by default are exactly the same as the physical coordinate system.
C. Viewport and window
1. Viewport-a arbitrarily specified rectangle in the physical coordinate system
2. Window-the logical coordinate system corresponds to the same rectangle in the physical coordinate system
Understand the view Port and window
1. The viewport and the window are the same rectangle in different coordinate systems
2. There is a one-to-one mapping relationship between the viewport and the coordinate points in the window
3. Port and window can be converted mutually through coordinate conversion
Qt--Basic graphics drawingQt--Basic graphics drawing
The transformation method of viewport and window
1. Define the viewport-the coordinates of the upper left corner, the coordinates of the lower right corner, calculate the width and height
2. Define the window-the coordinates of the upper left corner, the coordinates of the lower right corner, calculate the width and height
Code example

Widget.cpp
#include "Widget.h"
#include
#include
#include
#include

Widget::Widget(QWidget *parent)
: QWidget(parent)
{
}

void Widget::paintEvent(QPaintEvent *)
{
//Basic settings of drawing tools
QPainter painter(this);
QPen pen;

pen.setColor(Qt::green);
pen.setStyle(Qt::SolidLine);
pen.setWidthF(0.01);

painter.setPen(pen) ;

painter.setVie wport(50, 50, width()-100, height()-100);//View
painter.setWindow(-10, 2, 20, -4); //Window

painter.fillRect(-10, 2, 20, -4, Qt::black);

painter.drawLine(QPointF(-10, 0), QPointF(10, 0)); // x
painter.drawLine(QPointF(0, 2), QPointF(0, -2)); // y

for(float x=-10; x<10; x+=0.01)
{
float y = qSin(x);

painter.drawPoint(QPointF(x, y));
}

}

Widget::~Widget()
{

}

The running result is shown in the figure
Qt--Basic graphics drawing
C. Example implementation–simple drawing Program
The function we need-draw graphics freely, and be able to select graphics drawing colors
a. Interface selection
1. Take QWidget as the base class Create the main window of the drawing
2. Use QGroupBox to create the graphics setting area
3. Use the single-select QRadioBox to achieve the selection of the target graphics
4. Use the combo box QCombox to achieve the selection of the drawing color
b .The main realization of free drawing
1. Start with the mouse press, record the start coordinates–mousePressEvent
2. Record the pixel coordinates passed by the mouse when it moves–mouseMoveEvent
3. The mouse is released as the end, and the coordinates are recorded–mouseReleaseEvent
4. Between the two coordinates in the order of recording Draw a straight line–painEevent
c. The drawing of basic graphics is similar to free drawing
1. Start with a mouse press, and record the start coordinates–mousePressEvent
2. Move the mouse Each coordinate passed while moving is used as a temporary end coordinate–mouseMoveEvent
3. End with the mouse release, and determine the final end coordinate–mouseReleaseEvent
4. Draw the target graphic between the start coordinate and the end coordinate– The program running diagram implemented by painEevent
is as follows
Qt--Basic graphics drawingQt--Basic graphics drawing
Program source code link–https://down.51cto. com/data/2465109

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2787 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.