Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize basic graphic drawing with Qt

Shulou Source: shulou.com Published: 2022-06-02 11:12:53 10月04日 Update

This article mainly shows you "Qt how to achieve basic graphics drawing", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Qt how to achieve basic graphics drawing" this article.

one。 Basic graphics drawing

Key roles in A.Qt graphics system

A painter in QPainter--Qt who can draw all kinds of basic graphics and has brushes, brushes, and fonts for drawing.

The canvas in QPaintDevice--Qt, the painter's drawing board, and all the QWidget classes inherit from QPaintDevice

Key roles in Qt graphics system

Tools used in Qt

1.QPenn-used to draw the edges of geometry, consisting of parameters such as color, width, line style, etc.

2. QBrush-A color palette used to fill geometry, consisting of color and fill style

3. QFontt-for text drawing, made up of font properties

It is important to note that graphics can only be drawn in QWidget::paintEvent.

Coordinate system in B.Qt graphic system

1. Physical coordinate system-the position of the origin (0Power0) in the upper left corner, in pixels and the X coordinate grows to the right, and the Y coordinate grows downward.

two。 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 the graph, the size and position of the graph in the logical coordinate system are drawn on the specific device after conversion, and the logical coordinates are exactly the same as the physical coordinate system by default.

c. Viewports and windows

1. Viewport-an arbitrarily specified rectangle in the physical coordinate system

two。 Window-the same rectangle corresponding to the physical coordinate system in the logical coordinate system

In-depth understanding of viewports and windows

1. Viewports and windows are the same rectangle in different coordinate systems

two。 There is an one-to-one mapping relationship between viewports and coordinate points in the window.

3. Viewports and windows can be converted to each other by coordinate transformation

Transformation method between Viewport and window

1. Define viewports-upper left coordinates, lower right coordinates, calculate width and height

two。 Define the window-upper left coordinates, lower right coordinates, calculated width and height

Code example

Widget.cpp#include "Widget.h" # include # include Widget::Widget (QWidget * parent): QWidget (parent) {} void Widget::paintEvent (QPaintEvent *) {/ / basic settings of the drawing tool QPainter painter (this); QPen pen; pen.setColor (Qt::green); pen.setStyle (Qt::SolidLine); pen.setWidthF (.01); painter.setPen (pen) Painter.setViewport (50,50, width ()-100,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 Xerotel 10; x

Tags: Coordinates graphics coordinate systems viewports drawings foundations mouse logic physics color selection freedom content width upper left corner rectangle article system growth geometry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn macOS Shulou Information Shulou Technology