Questions tagged [qpainter]

QPainter is a Qt class that provides low-level painting on widgets or other paint devices.

QPainter provides optimized drawing functions that can draw lines, simple shapes, pixmaps, etc. QPainter can be used with any class that inherits QPaintDevice class.

A common use of QPainter is drawing inside a widget's paintEvent(). A simple example of usage will look like this:

 void someWidget::paintEvent(QPaintEvent *)
 {
     //creating a painter
     QPainter painter(this);
     //setting the pen color
     painter.setPen(Qt::red);
     //setting the font attributes
     painter.setFont(QFont("Arial", 30));
     //rendering text.
     painter.drawText(rect(), Qt::AlignCenter, "Qt");
 }

QPainter is highly customizable which allows a great variety in drawing capabilities.

Official documentation of this class can be found here for Qt 4.8 and here for Qt 5.

665 questions
3
votes
2 answers

Qt - circles for collision detection

I've been working on a physics simulation with circles in Qt. Thus far the easiest way to define circles I found is to make a QRect object and then draw the ellipse with that rectangle as a "blueprint". Now I've just got the problem that it paints a…
Tomathor
  • 41
  • 1
  • 9
3
votes
2 answers

Qt- fill circle with images

I've tried to fill circle with four images. Firstly , each photo brush in the same size ,hereafter scale final image with that size. But result is not what I want. At the moment circle in foreground and photos on background, like here: How to fill…
Farzan Najipour
  • 2,442
  • 7
  • 42
  • 81
3
votes
1 answer

Using QPainter with QImage in a loop not in a main thread

With this simple loop: for(int i=0;i
user2449761
  • 1,169
  • 13
  • 25
3
votes
1 answer

Invert or XOR a line in Qt

I can draw a solid-color line easily in Qt, but now I need to draw a line by inverting the original pixels, or perhaps XOR-ing the QBrush. I'm asking for the equivalent of SetROP2(R2_NOT) or SetROP2(R2_XORPEN) in Win32. Is that possible using Qt? I…
sashoalm
  • 75,001
  • 122
  • 434
  • 781
3
votes
1 answer

Connecting lines of different thickness to form a clean border using QPainter

I am trying to draw a rectangle. The color,thickness and the pen style for each border of the rectangle must be customizable. For example, I must be able to draw a rectangle with left border thickness say 5 and color being red, and right border…
Programmer
  • 1,290
  • 2
  • 12
  • 16
3
votes
1 answer

How to draw a QPixmap with rounded corners?

I have inherited from QGraphicsPolygonItem and would like to draw a QPixmap on the top of the item. The item itself and the pixmap should be drawn with rounded corners. How would you this?
Alexander Tyapkov
  • 4,837
  • 5
  • 39
  • 65
3
votes
1 answer

QPainter::drawImage() clips QImage when X, Y are not 0 on a QDeclarativeItem

I'm developing a new QML element in C++ based on this sample code. My class inherits from QDeclarativeItem and it's paint() method is responsible to draw a QImage to the screen: void NewQMLitem::paint(QPainter *painter, const…
karlphillip
  • 92,053
  • 36
  • 243
  • 426
3
votes
2 answers

QTimer with QPaintEvent

I've created a small QT application that redraws a circle at a random position. What I would like to do is repeat the method a predetermined number of times that draws the circle every second using a QTimer. I am not sure how to go about to…
Leo
  • 565
  • 5
  • 20
3
votes
1 answer

How to paint in a QMainWindow?

I have an extremely strange behaviour of a QPainter. In my 'MainWindow::paintEvent()' it works correctly: QPainter painter(this); qDebug() << "painter adress in paintEvent: " << (&painter); painter.setBrush(Qt::red); painter.drawRect(100, 100, 100,…
Bianfable
  • 237
  • 5
  • 13
3
votes
1 answer

Determine which pixels of QImage were changed while painting on it with QPainter

I have class Paintable that is able to paint itself with QPainter provided as an argument: class Paintable { public: virtual void paint(QPainter*) = 0; }; Instances of this class are being painted on a single QImage: QImage paint(const…
Oleg Andriyanov
  • 5,069
  • 1
  • 22
  • 36
3
votes
2 answers

What's painter.setViewport exactly do?

I know how painter.setWindow works. for example, if I maximize the widget larger, whatever I drew in that widget gets larger also in the same ratio. but I can't understand what painter.setViewport exactly do. Can anyone explain to me how it works…
Tito Tito
  • 248
  • 3
  • 10
3
votes
2 answers

QGradient ellipse blending

I am currently working on generating "heat-maps" with QPainter and QImage. My method consists of drawing multiple circles with black to transparent QRadialGradients as the QBrush (see "Intensity Map"). Then I apply a gradient map to the intensity…
ranger_rick
  • 120
  • 1
  • 7
3
votes
2 answers

QPainter::drawText, get bounding boxes for each character

I'm using QPainter to draw multiline text on QImage. However, I also need to display a colored rectangle around each character's bounding box. So I need to know the bounding box that each character had when being drawn. For example,…
sashoalm
  • 75,001
  • 122
  • 434
  • 781
3
votes
0 answers

Paintevent Consuming Huge amount of CPU in Qt

I have a problem with Paintevent in Qt. Paintevent thread is utilizing almost all amount of CPU when started. Code in constructor of my main widget. Display_dialog::Display_dialog(QWidget *parent):QDialog(parent), ui(new Ui::Display_dialog) { …
ScarCode
  • 3,074
  • 3
  • 19
  • 32
3
votes
2 answers

How to draw rounded corners with QPainter::drawPolyLine

I'm trying to create a custom container widget at the moment using QGroupBox as a base and drawing the new frame in the paint event, which is all working fine using drawPolyLine to create it, but I'd like to draw the frame with rounded corners. Has…
Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55