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 ?
QtWS25 Last Chance

How to paint to pixmap ?

Scheduled Pinned Locked Moved Unsolved Qt for Python
qpainterqwidget
12 Posts 3 Posters 8.1k Views
  • 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.
  • D Offline
    D Offline
    Dariusz
    wrote on 3 Nov 2019, 13:25 last edited by
    #1

    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 ")
    
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 3 Nov 2019, 14:47 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 3 Nov 2019, 16:51
      0
      • M mrjj
        3 Nov 2019, 14:47

        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 3 Nov 2019, 16:51 last edited by
        #3

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

        M 1 Reply Last reply 3 Nov 2019, 16:52
        0
        • D Dariusz
          3 Nov 2019, 16:51

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 3 Nov 2019, 16:52 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 3 Nov 2019, 17:03
          0
          • M mrjj
            3 Nov 2019, 16:52

            @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 3 Nov 2019, 17:03 last edited by Dariusz 11 Mar 2019, 17:04
            #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 :- )

            M 1 Reply Last reply 3 Nov 2019, 17:08
            0
            • D Dariusz
              3 Nov 2019, 17:03

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 3 Nov 2019, 17:08 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 3 Nov 2019, 17:40
              0
              • M mrjj
                3 Nov 2019, 17:08

                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 3 Nov 2019, 17:40 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

                M 1 Reply Last reply 3 Nov 2019, 18:08
                0
                • D Dariusz
                  3 Nov 2019, 17:40

                  @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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 3 Nov 2019, 18:08 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
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 3 Nov 2019, 18:20 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

                    M 1 Reply Last reply 3 Nov 2019, 18:26
                    1
                    • S SGaist
                      3 Nov 2019, 18:20

                      Hi,

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

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 3 Nov 2019, 18:26 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
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 3 Nov 2019, 18:28 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

                        M 1 Reply Last reply 4 Nov 2019, 07:44
                        1
                        • S SGaist
                          3 Nov 2019, 18:28

                          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

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 4 Nov 2019, 07:44 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

                          3/12

                          3 Nov 2019, 16:51

                          topic:navigator.unread, 9
                          • Login

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