Leaving out the duplicates we have the following draw operations - drawArc, drawChord, drawConvexPolygon, drawEllipse, drawLine, drawPath, drawPie, drawPoint, drawPolygon, drawPolyline, drawRect, drawRects and drawRoundedRect. Given the x1, y1, x2, y2 coordinates, the two QPoint objects would be defined as QPoint(x1, y1) and QPoint(x2, y2). The different call signatures are simply there for convenience. When you discover that a QLine itself is defined as QLine(const QPoint & p1, const QPoint & p2)or QLine(int x1, int y1, int x2, int y2)you see that they are all in fact, exactly the same thing. Ignoring the F-variants, we have 3 unique ways to draw a line - with a line object, with two sets of coordinates (x1, y1), (x2, y2) or with two QPoint objects. This is convenient if you have float positions as the result of other calculations, but otherwise not so much. If you're wondering what the difference is between a QLine and a QLineF, the latter has its coordinates specified as float. Methodĭraw a line between x1, y2 and x2, y2 ( int)ĭraw a line between p1 and p2 (both QPoint)ĭraw a line between p1 and p2 (both QPointF) The good news is that most of these are overloaded methods which are simply different ways of calling the same base methods.įor example, there are 5 different drawLine methods, all of which draw the same line, but differ in how the coordinates of what to draw are defined. QPainter provides a huge number of methods for drawing shapes and lines on a bitmap surface (in 5.12 there are 192 QPainter specific non-event methods). update() to trigger a refresh of the widget, but as we're drawing before the application window is shown a refresh is already going to occur. end() to close the painter and apply the changes. Save this to a file and run it and you should see the following - a single black line inside the window frame -Īll the drawing occurs within the draw_something method - we create a QPainter instance, passing in the canvas ( ()) and then issue a command to draw a line. Why do we use QLabel to draw on? The QLabel widget can also be used to show images, and it's the simplest widget available for displaying a QPixmap. To make this easy to demonstrate we'll be using the following stub application which handles creating our container (a QLabel), creating a pixmap canvas, displaying that in the container and adding the container to the main window.įrom PyQt5 import QtCore, QtGui, QtWidgets, uic This is a generic interface which can be used to draw on various surfaces including, for example, QPixmap. We'll go through some basic drawing operations and finally put it all together to create our own little Paint app.īitmap drawing operations in Qt are handled through the QPainter class. In this tutorial we'll take a look at QPainter - Qt's API for performing bitmap graphic operations and the basis for drawing your own widgets. Contrast with vector graphics, where the image is stored as a series of drawing instructions which are repeated to form the image. Once you understand how this works you can draw any widget you like!Ī bitmap is a rectangular grids of pixels, where each pixel is stored individually as a number of bits. All standard widgets draw themselves as bitmaps on a rectangular "canvas" that forms the shape of the widget. The first step towards creating custom widgets in PyQt5 is understanding bitmap (pixel-based) graphic operations.
0 Comments
Leave a Reply. |