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

Qt drawRect in background

I want to paint the background of a slider. I tried this but the color covers up the whole slider. This is in an inherited class of QSlider void paintEvent(QPaintEvent *e) { QPainter painter(this); painter.begin(this); painter.setBrush(/*not…
Geore Shg
  • 1,299
  • 5
  • 23
  • 38
4
votes
1 answer

PySide6 app crashes when using QPainter.drawLine()

On Windows 10, python3.10, PySide6 (or PyQt6) QApplication crashes when calling QPainter.drawLine() . The terminal just displays : Process finished with exit code -1073741819 (0xC0000005) Please find below the code: import sys from PySide6.QtCore…
u2gilles
  • 6,888
  • 7
  • 51
  • 75
4
votes
1 answer

QPainter composition mode example does not work as expected

I'm stuck with the difference from the book example and my version of it. Qt version 5.12.0 As it's shown in the example: As I see from my output: First, destination and source In/Atop modes have not the same pictures. And, another noticed thing…
Anton
  • 185
  • 1
  • 14
4
votes
1 answer

Set the fill color (not the stroke color) for the QPainter in QT

How could I set the fill color (not the stroke color) for the QPainter in QT? For example, I have a code which is responsible for filling the rectangle. It looks like: painter.fillRect(fillRect, Qt::SolidPattern); Where the type of painter is…
oobarbazanoo
  • 397
  • 1
  • 5
  • 15
4
votes
1 answer

"QPainter::begin: Paint device returned engine == 0, type: 1"

I have the following test code: import sys from PySide.QtGui import * app = QApplication(sys.argv) widget = QWidget() painter = QPainter(widget) Upon creating the QPainter object, I get the error message: QPainter::begin: Paint device returned…
HelloGoodbye
  • 3,624
  • 8
  • 42
  • 57
4
votes
1 answer

Using QPainter to draw a line between QWidgets

I'm working on an application where I need to be able to draw a line between two QWidget objects. I have tried quite a few things, but my current attempt (which I think is in the right direction I just think I'm missing something) is to have the…
kadd11
  • 85
  • 1
  • 5
4
votes
1 answer

Qt drawing a ring / circle with a hole

I need to draw a circle with QPainter. When I used drawEllipse function like : void UserClass::Draw(QPainter &painter) { painter.save(); painter.setBrush( GetColor() ); QPoint centerPosition = GetCenterPosition(); …
Kadir Erdem Demir
  • 3,531
  • 3
  • 28
  • 39
4
votes
1 answer

Custom circular progressbar in Qt

I'm able to do circular progressbar in qt while overriding paintEvent of the widget and draw circular progressbar but having a difficulty with the second circular progressbar (not able to draw like this) behind main circular progressbar: Can…
elgolondrino
  • 665
  • 9
  • 33
4
votes
2 answers

How to use QPainter in a QWidget in PyQt4

My code below currently opens a 500x500 QMainWindow that is blank. I am simply trying to draw a circle in the QWidget using QPainter. Here is my code: from PyQt4 import QtCore, QtGui, Qt from PyQt4.QtGui import QApplication, QMainWindow import…
Zach G
  • 597
  • 2
  • 10
  • 23
4
votes
1 answer

Conflict between shaders and QPainter in paintGL() for rendering 2D text

I have the following class inheriting from QOpenGLWidget and QOpenGLFunctions: class OpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions { Q_OBJECT public: OpenGLWidget(); virtual ~OpenGLWidget(); void initializeGL(); …
Grégoire Borel
  • 1,880
  • 3
  • 33
  • 55
4
votes
1 answer

Qt5 scribble with layers - QPaintDevice: cannot destroy device that is being painted

I'm trying to re-implement the scribble demo app for multiple layers of images. I am struggling to draw into the pixmap within the scene as the Painter complains that is being destroyed to early. QPaintDevice: Cannot destroy paint device that is…
jonathanbsyd
  • 8,031
  • 6
  • 24
  • 26
4
votes
0 answers

How to draw a line with a custom round pen in Qt

I have Tile and code for generate polyline: bool eventFilter(QObject *obj, QEvent *event) { if(event->type() == QEvent::Paint) { QWidget *widget = qobject_cast(obj); QBrush brush; …
Maper
  • 41
  • 3
4
votes
1 answer

How to draw a line over widget in Qt5

I have a mainwindow and more than two widget on mainwindow. I wanna draw lines on the whole mainwindow and when the line should be over the widgets not behind these widgets. I've tried overwrite paintevent function of mainwindow and draw a line…
Asuka
  • 301
  • 4
  • 16
4
votes
3 answers

QTableView, setting a cell's font and background colour

I am using QTableView and QStandardItemModel and I'm trying to colour a row with the font remaining black. I am using my delegate class's paint method: void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex…
ethane
  • 2,329
  • 3
  • 22
  • 33
4
votes
1 answer

Center text vertically when drawing with QPainter's drawText()

My strategy when centering text on images is to get bounding rectangle for that text and divide width or height by two. I did the same in this case. This is example I have created: void CanvasWidget::paintEvent(QPaintEvent*) { //Create image: …
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778