Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to paint to pixmap ?
Forum Updated to NodeBB v4.3 + New Features

How to paint to pixmap ?

Scheduled Pinned Locked Moved Unsolved Qt for Python
qpainterqwidget
12 Posts 3 Posters 8.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    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);
    }
    

    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 ?

    self.l.setPixmap(m)

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

    D 1 Reply Last reply
    0
    • mrjjM mrjj

      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);
      }
      

      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 ?

      self.l.setPixmap(m)

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

      D Offline
      D Offline
      Dariusz
      wrote on last edited by
      #3

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

      mrjjM 1 Reply Last reply
      0
      • D Dariusz

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

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

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

        D 1 Reply Last reply
        0
        • mrjjM mrjj

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

          D Offline
          D Offline
          Dariusz
          wrote on last edited by Dariusz
          #5

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

          mrjjM 1 Reply Last reply
          0
          • D Dariusz

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #6

            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.

            D 1 Reply Last reply
            0
            • mrjjM mrjj

              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.

              D Offline
              D Offline
              Dariusz
              wrote on last edited by
              #7

              @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

              mrjjM 1 Reply Last reply
              0
              • D Dariusz

                @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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @Dariusz

                Hi
                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.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Hi,

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  mrjjM 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    Hi,

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

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

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

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      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

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      mrjjM 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        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

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved