Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.
QPainter not active
undefined last edited by A Former User
I am making a simple program in Python with PyQT 5 for GUI, which is supposed to draw a square onto the screen.
However, I am quite confused as to how I am supposed to set up my QPainter.
I have the following code:
from PyQt5 import QtWidgets, QtGui, QtCore import sys def paintEvent(self, x, y, color, event = None): global qp qp = QtGui.QPainter(self); qp.begin(self) qp.setPen(QtGui.QColor(int(color, 16))) qp.setBrush(QtGui.QColor(int(color, 16))) qp.drawRect(x, y, 25, 25) qp.end() def window(title, width, height): app = QtWidgets.QApplication(sys.argv) w = QtWidgets.QWidget() w.setFixedSize(width, height) w.setWindowTitle(title) paintEvent(w, 0, 0, 'ffffff') w.show() sys.exit(app.exec_()) window('test', 120, 120)
But this gives me:
QWidget::paintEngine: Should no longer be called QPainter::begin: Paint device returned engine == 0, type: 1 QWidget::paintEngine: Should no longer be called QPainter::begin: Paint device returned engine == 0, type: 1 QPainter::setPen: Painter not active QPainter::setBrush: Painter not active QPainter::drawRects: Painter not active QPainter::end: Painter not active, aborted
Can someone point me in the right direction?
P.S First post. new to Qt - don't be harsh ;)
Hi and welcome to devnet,
If you want to do custom painting, you have to create a custom QWidget subclass and reimplement the paintEvent method of that class. You can't just freely paint from anywhere at any time.
undefined last edited by
I don't quite know how to do that, could you expand on that, please?
A quick google search returns this article which covers most aspects of this subject.