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
45
votes
1 answer

Efficient off-screen rendering of QPainterPaths (OpenGL and non-OpenGL solution required)

In my application, I paint a street map using QPainter on a widget made by QPainterPaths that contain precalculated paths to be drawn the widget is currently a QWidget, not a QGLWidget, but this might change. I'm trying to move the painting…
leemes
  • 44,967
  • 21
  • 135
  • 183
33
votes
2 answers

Qt drawing a filled rounded rectangle with border

I want to draw a rectangle with rounded corners (border radius same for all 4 corners) with a specific color filling the entire rectangle, and a separate border color (say border is 1 px wide). From my observation, Qt provides three methods -…
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
28
votes
4 answers

How to make Qt widgets fade in or fade out?

I am trying to fade in and fade out a QLabel or for that matter any QWidget subclass. I have tried with QGraphicsEffect, but unfortunately it works well only on Windows and not on Mac. The only other solution which can work on both Mac & Windows…
Soumya Das
  • 1,635
  • 2
  • 19
  • 28
21
votes
2 answers

How to get the sizes of the rendered text on a QPainter?

I draw in my Qt program on a QPainter the text and various elements round it. I need to get the sizes in pixels which will be occupied by this text. Can I get somehow the sizes in pixels, knowing a text string and a font? Thanks.
shau-kote
  • 1,110
  • 3
  • 12
  • 24
19
votes
3 answers

Draw rich text with QPainter

is there a way to draw fixed text that has subscripts. My goal is to have something like: "K_max=K_2 . 3" QString equation="K_max=K_2 . 3"; painter.drawText( QRect(x, y , width, y+height), Qt::AlignLeft|Qt::AlignVCenter, equation); I also tried…
luffy
  • 2,246
  • 3
  • 22
  • 28
19
votes
2 answers

How to align QPainter drawText around a point, not a rectangle?

I want to set text drawing alignment using one point as coordinate, not a rectangle. As far as I understand QPainter::drawText allows to set text alignment only when I pass coordinates as rectangle. How can I set text alignment if I wish to align…
Ufx
  • 2,595
  • 12
  • 44
  • 83
19
votes
2 answers

How to use QPainter on QPixmap

I'm a newbie to Qt/Embedded. I want to use QPainter to draw stuff on a QPixmap, which will be added to QGraphicsScene. Here is my code. But it does not show the drawings on the pixmap. It shows only the black pixmap. int main(int argc, char **argv)…
manmatha.roy
  • 577
  • 1
  • 9
  • 22
18
votes
5 answers

Qt/C++ : drawing efficiently

I have designed a program which, basically, cuts a geometrical shape into many small triangles (in a "left canvas"), applies some simple mathematical transformation to the bunch of triangles, and redraws them in their new configuration. See screen…
Seub
  • 2,451
  • 4
  • 25
  • 34
15
votes
1 answer

How to use a mask with QPainter?

I have a shape (in blue) loaded from a PNG with transparency: Then I'm drawing several circles on top of this shape (in red) with QPainter::drawEllipse. The result of that is somewhat similar to the third picture with the red shape completely…
laurent
  • 88,262
  • 77
  • 290
  • 428
15
votes
4 answers

Use window/viewport to flip QPainter y-axis

I'm using Qt 4.7 QPainter to draw some polygons, etc into a widget. I am hoping to alter the coordinate system so that (0,0) is at the center of my widget, and the x/y axis behave in a standard "Cartesian" way (ie. y increases going "up" and…
sidewinderguy
  • 2,394
  • 4
  • 24
  • 24
13
votes
3 answers

Render QWidget in paint() method of QWidgetDelegate for a QListView

i'm having difficulties implementing custom widget rendering in a QListView. I currently have a QListView displaying my custom model called PlayQueue based on QAbstractListModel. This is working fine with simple text, but now I would like to display…
Adrien Rey-Jarthon
  • 1,015
  • 2
  • 9
  • 22
13
votes
4 answers

Qt drawing icons using color and alpha-map

I would like to draw icons (only one color) in different colors. To do so, I would like to import a single alpha-texture and then combine this with a given color in the application. The result should be, that nothing is drawn on to the background,…
ruhig brauner
  • 943
  • 1
  • 13
  • 35
12
votes
1 answer

Qt: Drawing high DPI QPixmaps

I have written application that draws two smiling faces: First one is painted directly on QWidget: void DirectFace::paintEvent(QPaintEvent *ev) { QPainter painter(this); paintFace(painter); } Second one is painted on a QPixmap, which in…
12
votes
3 answers

Qt Charts rendering problems on a PDF

I'm using Qt charts module to draw a pie chart directly on a PDF file. Here's the problem: For some unknown reason, the chart needs to be displayed with show() before it's rendered to the PDF for it's size to be OK (left image). On the other…
karlphillip
  • 92,053
  • 36
  • 243
  • 426
12
votes
2 answers

QtPainter Error Paint device returned engine ==0, type 3 ,Painter not active

I'm trying to paint some points of my image and I don't know why it doesn't work. I have defined a QImage and I want to modify some points. QImage *cou= new QImage(height,largeur,QImage::Format_Mono); cou->fill(1); QPainter *fig=new QPainter…
user2269556
  • 143
  • 2
  • 2
  • 11
1
2 3
44 45