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

pyqt print preview QTableView

I'm trying to print the contents of a QTableView to the printer . i implement bottom script and it run successfully without any error. but all data are Unusual in printPreview. this is pic from my problem -FOUNTAIN def…
iraj jelodari
  • 3,118
  • 3
  • 35
  • 45
2
votes
1 answer

Why do my my line widths looks different in a QGraphicsScene with the same QPen width?

I use a QPainter to draw my widget with this code: QPen pen(Qt::black, 0.6, Qt::SolidLine); QPainter painter(this); painter.setPen(pen); // vertical painter.drawLine(startX,0,startX,50); painter.drawLine((startX +=…
nils
  • 97
  • 1
  • 8
2
votes
1 answer

How do I destroy the QPainter object / Get rid of drawText() memory leak?

I'm trying to trim down the memory leaks of a large application. Using valgrind, I also saw many instances of memory leaks when calling the drawText() function of the QPainter class. It could be a Qt bug according to some sources that I've read, but…
Owen
  • 4,063
  • 17
  • 58
  • 78
2
votes
2 answers

QPainter colored text (syntax coloring)

I have a custom Qt widget which I used to display disassembly and I am looking to add syntax coloring to it. Currently, I simply set the QPen to a solid color, construct the text I want to display, and render it to the QPainter at the appropriate…
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
2
votes
2 answers

Qt Beginner: QPainter widget not rendering

I don't have much experience with Qt and I am having trouble using QPainter. I am trying to make a simple graphing widget which takes in a number of points and to create a QVector of QPoints, and then uses this vector to draw a polygon. However,…
user269857
2
votes
1 answer

Using an alpha transparent mask on a QWidget?

Is it possible to assign an alpha-transparent mask to a QWidget? I know how to set a mask using setMask but it seems it only supports black&white masks. Is it possible to make it support a true alpha channel? i.e. currently I have a PNG like…
laurent
  • 88,262
  • 77
  • 290
  • 428
2
votes
0 answers

QPainter QColorDialog Unexpected Values (Colorspace issue?)

I'm using PySide6 on MacOS Catalina (10.15.7). I was trying to do something with QColorDialog and getting some unexpected values. I believe this must be due to some colorspace conversions, but I'm not clear on what is happening or how to correct…
J Bones
  • 627
  • 6
  • 13
2
votes
1 answer

An auto-scaled on-off background image in a QLineEdit

I have a couple of QLineEdit widgets that need to have their backgrounds appear and disappear upon certain code changes, and need those backgrounds to also rescale when widget size changes. I am getting quite lost in all the stackoverflow and…
S. B.
  • 63
  • 4
2
votes
0 answers

QPainter.drawText output is blurry

I'm trying to create a system tray icon to display a string ("Hello world!" in the code snippet below) using QPainter's drawText. The problem is that the text is not crisp, and is rather blurry (I have a HiDPI monitor if that's relevant). Here's a…
Blademaster
  • 324
  • 2
  • 15
2
votes
2 answers

QPainter redraw on window gaining/losing focus

I am learning Qt and am trying to paint a simple display for my program with QPainter. I draw static elements (frames etc.) once and only update dynamic elements afterwards. Everything works fine, except for when the window loses focus. As soon as…
proarunas
  • 83
  • 10
2
votes
1 answer

QPainter rotation prevents correct QPixmap rendering

Reported to Qt as a bug: https://bugreports.qt.io/browse/QTBUG-93475 I am re-drawing a QPixmap multiple times in different locations, with differnt rotations by transforming the QPainter. In certain situations the QPixmap is not drawing correctly.…
Troyseph
  • 4,960
  • 3
  • 38
  • 61
2
votes
2 answers

Qt5 : Displaying a Qpainter with layouts

I started using Qt5 a little while ago and I don't know how to set the position of my drawing in my window. I have a Drawing class which is a QWidget and which contains my paintEvent() function and other functions; and a MainWindow class which…
Arcane
  • 33
  • 4
2
votes
1 answer

Objects are transparent when using QPainter

So I've tried to put labels on my objects. I tried out a test example to just put one simple label there. When I ran the program, however, all the objects were somewhat transparent. paintGL: void mainWidget::paintGL(){ QPainter painter(this); …
new Q Open Wid
  • 2,225
  • 2
  • 18
  • 34
2
votes
2 answers

How can QPainter performance be improved?

I have a large display (about 1000x2000 pixels) and I'm doing the following to draw images to the screen: QImage *pImage = GetImage(); // from wherever QPainter painter(this); painter.drawImage((0,0), *pImage); // this line takes over 100ms to…
glutz
  • 1,889
  • 7
  • 29
  • 44
2
votes
1 answer

Qt C++ QLine QPoint

I have QVector m_vertices and QVector in my drawingWidget.h I currently trying to do a simple drawing tool, where I can add vertices and lines. I managed to draw multiple vertices on my "MainWindow", but I can't really understand, how…
Vlad Paskevits
  • 388
  • 2
  • 12