How to paint to pixmap ?
-
Hey
I was trying to paint some icons/graphics to a pixmap for later usage but I either crash in python with no error or just fail.. how can I do this ?
class myPainter(QWidget): def __init__(self): super().__init__() self.iconList = {} self.l = QLabel("ascas") self.l.show() def drawIcons(self, val): if type(val) == list: for a in val: self.draw(a) else: print(val) self.draw(val) def draw(self, iconMode): m = QPixmap(500, 500) m.fill(Qt.yellow) p = QPainter(m) if iconMode == 0: p.setBrush(Qt.red) p.drawRect(QRect(0, 0, 100, 100)) self.l.setPixmap(m) print("Drawing 0 ")
-
Hi
It seems ok. ( as far as i can read python)
You create pixmap with some size. Assign the painter to it via its
ctor.This surely paints
int GetBarHeight(int MAX) { return rand() % (MAX - 5) + 5; } void MainWindow::on_pushButton_released() { int h = ui->label->height(); int w = ui->label->width(); QPixmap pix(w, h); QPainter paint(&pix); pix.fill( Qt::white ); paint.setPen(QColor(0, 0, 0, 255)); int y = 0; int x = 0; int bw = 10; // bar width for (int barcount = 0; barcount < 12; ++barcount) { paint.setBrush(QColor(255 - x, 34 + x, 255, 255)); paint.drawRect(x, h - GetBarHeight(h), bw, h ); x += bw + 4; } paint.end(); ui->label->setPixmap(pix); }
The crashing part is ODD. i though python was pretty safe int that reagrds.
In any case, you seem to assign it to a label ?
self.l.setPixmap(m)
Is the l valid ?
I assume you can have dangling pointers too in pyt. -
@Dariusz
Ok it was not dangling pointer then. :)
I tried for fun making invalid pixmap but i could make it crash.
Maximum effect was nothing shown.
So i have a feeling the crash might have been a bit unrelated to the pixmap.But if this code also crashes, we might found some binding bug :)
-
Hi
is
m = QPixmap(500,500)like (c++)
QPixmap m(500,500);or
QPixmap *m = new QPixmap (500,500);Does it also crash there if you make new project and does nothing but
m = QPixmap(500,500)If you can make a small example that crash, its ok to open bug report.
-
Hi,
Add
p.end()
when you're done painting. So in your code, before you callsetPixmap
. -
I don't know how the binding manages that but when testing the code, I got the following message:
QPaintDevice: Cannot destroy paint device that is being painted