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
8
votes
2 answers

Draw a line and curves with fading edges with QPainter

QPainter is very easy to use and to draw a line one would simply do this: QPainter painter(&image); QPen pen; pen.setWidth(5); pen.setColor("black"); painter.setPen(pen); painter.drawLine(QPointF(0,0), QPointF(200,250)); Now this works well, but I…
reckless
  • 741
  • 12
  • 53
7
votes
3 answers

How to rotate a QPushButton?

I would like---with Python and Qt4---to rotate a QPushButton (or at least its text) so it can stand vertically. I've seen some documentation online, but I couldn't make much sense out of it---it's in C and I'm C-illiterate. From what I read though,…
neydroydrec
  • 6,973
  • 9
  • 57
  • 89
7
votes
1 answer

Qt & OpenGL - QGLWidget & QPainter: depth buffering

I subclass QGLWidget and have my painting code in paintEvent instead of paintGL as I want to paint a 2D overlay using QPainter over my 3D stuff done with OpenGL. My depth buffering works fine when I don't have an overlay. If the overlay is painted,…
user816098
  • 262
  • 3
  • 14
7
votes
1 answer

PyQt5 triggering a paintEvent() with keyPressEvent()

I am trying to learn PyQt vector painting. Currently I am stuck in trying to pass information to paintEvent() method which I guess, should call other methods: I am trying to paint different numbers to a fundamental block (here drawFundBlock()…
HammieTime
  • 73
  • 1
  • 1
  • 4
7
votes
3 answers

How to make a QImage or QPixmap semi-transparent - or why is setAlphaChannel obsolete?

4.7 and like to overlay two images on a qgraphicsview. The image on top shall be semi-transparent to allow to see through it. Initially both images are fully opaque. I expected some function for setting a global alpha-value for each pixel to exist,…
FFox
  • 1,550
  • 2
  • 17
  • 26
7
votes
3 answers

Text from QPainter much nicer than from QPainterPath

I want to draw text using QPainter, and I want to use QPainterPath first (because ultimately I want to rotate the text in all sorts of ways). However, I find that the text produced by QPainterPath is much uglier than the text produced by…
Yellow
  • 3,955
  • 6
  • 45
  • 74
7
votes
4 answers

How to rotate text for drawText?

I would like to rotate the text 45 degrees? QFont font; font.setPixelSize(12); //grid for(int i = 0; i < 10; i++){ painter->drawLine(100, 100 + i * 800/9, 900, 100 + i * 800/9); str = QString::number((double)9 - i, 'd', 1); …
jdl
  • 6,151
  • 19
  • 83
  • 132
7
votes
2 answers

How to draw custom shapes in Qt with QPainter or QPainterPath using one shape or a group of shapes joined

How can I draw a shape like a tear? I need to draw without using more than one shape (an ellipse and a polygon) because QPen will draw for each shape. I need to join shapes to create a new one, or tell QT to join the border across both shapes,…
6
votes
1 answer

Make an animated wave with drawPolyline in PySide/PyQt

I'm trying to animate a polyline (it have to act like a wave). I've tried this way: from PySide.QtCore import * from PySide.QtGui import * import sys, time class Test(QMainWindow): def __init__(self, parent=None): …
SaulTigh
  • 913
  • 2
  • 14
  • 27
6
votes
1 answer

QPainter width and height

Is there any chance to find out the size of the QPainter? I am using QPainter for drawing the whole graphic interface for a mobile app. The problem is that certain application overlay the window with menu which size is different for every device,…
Blackie123
  • 1,271
  • 4
  • 16
  • 22
6
votes
1 answer

QPainter rotation. Where to translate?

I'm working on a new project in Qt, using QPainter to draw a QWidget. The problem is, when I try to rotate QPainter the text I want to draw rotates out of my QWidget. I know how to solve the problem in general, but somehow I couldn't figure it out…
Flashcap20
  • 105
  • 1
  • 7
6
votes
1 answer

QPainter::drawPixmap() doesn't look good and has low quality?

I'm trying to draw an icon(.png) inside a QWidget with QPainter::drawPixmap() : QPixmap _source = "/.../.png"; painter.setRenderHint(QPainter::HighQualityAntialiasing); painter.drawPixmap(rect(), _source); but in comparing to QLabel (for example)…
IMAN4K
  • 1,265
  • 3
  • 24
  • 42
6
votes
1 answer

How to select a region with QRubberBand on a QLabel like in KSnapshot?

I am writing a screenshot utility with PyQt, and the idea is take a screenshot of the whole desktop, then display it in a QLabel, make the window full screen and user selects a region by mouse. Is it possible to do this efficiently with a QLabel? I…
Shuman
  • 3,914
  • 8
  • 42
  • 65
6
votes
1 answer

2d HUD not drawing properly over QGLWidget (using QPainter)

I am trying to display HUD over my 3D game. For starters, I am just trying to display "Hello World", but I haven't had any success yet! The scene freezes / flickers once I am done. I am using Qt/C++ and QGLWdiget / QPainter to get this done. I have…
brainydexter
  • 19,826
  • 28
  • 77
  • 115
6
votes
3 answers

Determine bounding rect of line in Qt

I am drawing a line using QPainterPath between two points as follows: QPainterPath line; line.moveTo(start_p); line.lineTo(end_p); QPen…
Kamalpreet Grewal
  • 822
  • 1
  • 13
  • 28
1 2
3
44 45