Qt World Summit: Register Today!

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):
            self.iconList = {}
            self.l = QLabel("ascas")
        def drawIcons(self, val):
            if type(val) == list:
                for a in val:
        def draw(self, iconMode):
            m = QPixmap(500, 500)
            p = QPainter(m)
            if iconMode == 0:
                p.drawRect(QRect(0, 0, 100, 100))
                print("Drawing 0 ")

  • Lifetime Qt Champion

    It seems ok. ( as far as i can read python)
    You create pixmap with some size. Assign the painter to it via its

    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;

    alt text

    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 ?


    Is the l valid ?
    I assume you can have dangling pointers too in pyt.

  • @mrjj Thanks for example & checking it! I created label in constructor, will try ur code next, thanks!

  • Lifetime Qt Champion

    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 :)

  • @mrjj Running this code causes crash:

        def draw(self, iconMode):
            m = QPixmap(500,500)

    Can't initialize QPixmap() with size.

    If you could reproduce the crash, then we could post it to bug report :- )

  • Lifetime Qt Champion

    m = QPixmap(500,500)

    like (c++)
    QPixmap m(500,500);

    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.

  • @mrjj I did some tests. I looks like it crashes when I do this p= QPainter(m), so passing it as source to QPainter o.O

  • Lifetime Qt Champion


    well M seems to be a class member so
    out of scope issues should not be possible.

    So it could be a real bug if you can reproduce it in a minimal program.

  • Lifetime Qt Champion


    Add p.end() when you're done painting. So in your code, before you call setPixmap.

  • Lifetime Qt Champion

    Good call.
    It still works without painter.end();
    in the c++ version but could be critical for a binding.

  • Lifetime Qt Champion

    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

  • Lifetime Qt Champion

    Ok that sounds a bit bad like -
    "delete while in use" situation.
    So it seems plausible it could crash from that in some cases.

Log in to reply