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

How to draw a rectangle and adjust its shape by drag and drop in PyQt5

I'm trying to draw a rectangle on GUI created by PyQt5 by drag and drop. I managed to do that, but the rectangle is drawn when the mouse left key is released. What I want to do is like this link: When the mouse left button is pressed, start…
pothny3
  • 138
  • 1
  • 2
  • 7
11
votes
2 answers

Using QPainter over OpenGL in QGLWidget when using shaders

Many of you Qt (4.6 specifically) users will be familiar with the Overpainting example supplied in the OpenGL tutorials, I'm trying to do something very similar but using shaders for the pure OpenGL data, instead of the old fixed-function…
cmannett85
  • 21,725
  • 8
  • 76
  • 119
10
votes
1 answer

QPainter not active

The following code results in a bunch of errors: void MainWindow::displayBoard() { QPixmap pix(0,0); pix.fill(Qt::white); QPainter painter(&pix); painter.setBrush(Qt::black); for(int row = 0; row < 8; row++) for(int col =…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
10
votes
1 answer

Draw text on image using Qt

I want to draw text on an image. I use this code, but I do not see any text on the image. void ImageSaver::save(const QString &path) const { QImage image(img_); QPainter p(&image); p.setPen(QPen(Qt::red)); p.setFont(QFont("Times",…
neda
  • 329
  • 1
  • 4
  • 15
10
votes
1 answer

How to draw single-colour Ellipse (no black border) with QPainter

Code for the beginning: QColor yellow("#f0d048"); Qt::BrushStyle style = Qt::SolidPattern; QBrush brush(yellow, style); painter.setBrush(brush); painter.drawEllipse(10,10,10,10); Everytime I do this, I get a yellow circle surrounded by a black…
Aragon
  • 183
  • 2
  • 8
10
votes
3 answers

QPainter. Draw line

I am trying to draw line. int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); QPainter painter(&w); painter.setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap)); painter.drawLine(0,…
Ufx
  • 2,595
  • 12
  • 44
  • 83
10
votes
2 answers

How to draw a linear gradient arc with Qt QPainter?

I'm trying to develop a custom QProgressBar that will look like the following image : I created a class that extends QProgressBar and implemented the paintEvent() : void CircularProgressBar::paintEvent(QPaintEvent*) { int progress =…
Jesse J
  • 532
  • 1
  • 7
  • 18
10
votes
3 answers

How to draw and fill a triangle with QPainter?

This is what I tried, it gave me no output. Where am I going wrong? // Start point of bottom line qreal startPointX1 = 600.0; qreal startPointY1 = 600.0; // End point of bottom line qreal endPointX1 =…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
10
votes
2 answers

How to draw a line with animation in PyQt4

I have a list of points. For example, points = [[160, 75], [115, 567]]. How to draw a line in PyQt4, so it would be something like this: Thanks in advance. EDIT: For the record, I'm trying to implement Bezier Curves, so it looked like this: Here…
SaulTigh
  • 913
  • 2
  • 14
  • 27
9
votes
1 answer

QPainter keep previous drawings

This is my first time using Qt and I have to make a MSPaint equivalent with Qt. I am however having trouble with painting my lines. I can currently draw a line by clicking somewhere on the screen and releasing somewhere else, however when I draw a…
anaBad
  • 309
  • 2
  • 13
9
votes
3 answers

Qt: QWidget::paintEngine: Should no longer be called

I'm trying to make an app where you can draw with your finger on a canvas. To achieve this, I'm subclassing QWidget as MFCanvas, registered the class in QML with qmlRegisterType<>(), implementing the virtual paintEvent(); function, and drawing on…
PH-zero
  • 293
  • 1
  • 4
  • 15
9
votes
5 answers

Qt Beginner QPainter and QRect

How would I go about drawing a rectangle? I have tried two different ways; void MyWidget::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.setPen(Qt::black); QRect rect =…
Ash
  • 125
  • 1
  • 1
  • 9
9
votes
2 answers

Drawing a line on a QWidget

I'm attempting to create a widget that consists of of a single horizontal black line. This widget will be placed in a QGridLayout such that it takes up the entire row, thus acting as a separator. As the widget upon which the layout is installed is…
user360907
8
votes
2 answers

How does Qt draw a border around a rectangle?

I want to know how Qt does a border when using QPainter's drawRect. The reason for this is I am trying to draw three rectangles next to each other, but I'm having trouble getting them to touch perfectly at all pen sizes.
Serodis
  • 2,092
  • 4
  • 25
  • 34
8
votes
2 answers

QPainter::rotate disables antialiasing of drawn text

I use QPainter::setRenderHint(QPainter::Antialiasing, true) to tell Qt that I want it to antialias any drawing I do (in this case, text drawn with drawText()). This works fine and the text looks good, until I want to rotate the pixmap I'm drawing to…
sam-w
  • 7,478
  • 1
  • 47
  • 77
1
2
3
44 45