Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QPainter to draw on a QPixmap and save it
Forum Updated to NodeBB v4.3 + New Features

Using QPainter to draw on a QPixmap and save it

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 843 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.
  • canellasC Offline
    canellasC Offline
    canellas
    wrote on last edited by
    #1

    Based on a example (that I can not find again) that show the use of a QPainter and `QPdfWriterP , I am trying to create a PNG file like this:

    void MainWindow::on_pushButton_5_clicked() {
      QPixmap paintDevice;
      QPainter painter(&paintDevice);
    
      int x0{10};
      int y0{10};
    
      const int width{200};
      const int height{200};
      const int cols{5};
      const int rows{3};
    
      QPen line_pen(Qt::black);
      line_pen.setWidth(5);
      painter.setPen(line_pen);
       
      // calls to drawLine, setPen e setFont among other methods of  `QPainter`
    
      const QString fileName("/var/tmp/mydoc.png");
      paintDevice.save(fileName, "PNG");
    }
    

    But the file /var/tmp/mydoc.png is not generated, and the messages QPainter::setFont: Painter not active and QPainter::setPen: Painter not active are displayed.

    I tried to find an example that showed QPainter and QPixmap used this way, but I failed to find.

    Does anyone have an example or reference showing how to do this?

    TIA

    Christian EhrlicherC JonBJ 2 Replies Last reply
    0
    • canellasC canellas

      @JonB

      The code that actually draws was removed to reduce the size of the code.

      The line that saves the file is executed, and the files at var/tmp are not deleted by the OS automatically.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #7

      @canellas Start by checking return result of QPixmap::save(). I suppose it's false.

      Is paintDevice still a null QPixmap? I think it is, depending on what code you removed. Hence earlier
      @Christian-Ehrlicher said in Using QPainter to draw on a QPixmap and save it:

      Your QPixmap is null. You should pass a valid QPixmap to the painter.

      I think you have to pass a QPixmap big enough for your painting.

      Then maybe the save() will do something. It wants something valid to save.

      canellasC 1 Reply Last reply
      1
      • canellasC canellas

        Based on a example (that I can not find again) that show the use of a QPainter and `QPdfWriterP , I am trying to create a PNG file like this:

        void MainWindow::on_pushButton_5_clicked() {
          QPixmap paintDevice;
          QPainter painter(&paintDevice);
        
          int x0{10};
          int y0{10};
        
          const int width{200};
          const int height{200};
          const int cols{5};
          const int rows{3};
        
          QPen line_pen(Qt::black);
          line_pen.setWidth(5);
          painter.setPen(line_pen);
           
          // calls to drawLine, setPen e setFont among other methods of  `QPainter`
        
          const QString fileName("/var/tmp/mydoc.png");
          paintDevice.save(fileName, "PNG");
        }
        

        But the file /var/tmp/mydoc.png is not generated, and the messages QPainter::setFont: Painter not active and QPainter::setPen: Painter not active are displayed.

        I tried to find an example that showed QPainter and QPixmap used this way, but I failed to find.

        Does anyone have an example or reference showing how to do this?

        TIA

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Your QPixmap is null. You should pass a valid QPixmap to the painter.

        canellasC 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          Your QPixmap is null. You should pass a valid QPixmap to the painter.

          canellasC Offline
          canellasC Offline
          canellas
          wrote on last edited by
          #3

          @Christian-Ehrlicher

          I understood QPixmap default constructor created a valid object, that could be used by QPainter as a "drawing surface".

          Would you be kind to explain how should I do it?

          1 Reply Last reply
          0
          • canellasC canellas

            Based on a example (that I can not find again) that show the use of a QPainter and `QPdfWriterP , I am trying to create a PNG file like this:

            void MainWindow::on_pushButton_5_clicked() {
              QPixmap paintDevice;
              QPainter painter(&paintDevice);
            
              int x0{10};
              int y0{10};
            
              const int width{200};
              const int height{200};
              const int cols{5};
              const int rows{3};
            
              QPen line_pen(Qt::black);
              line_pen.setWidth(5);
              painter.setPen(line_pen);
               
              // calls to drawLine, setPen e setFont among other methods of  `QPainter`
            
              const QString fileName("/var/tmp/mydoc.png");
              paintDevice.save(fileName, "PNG");
            }
            

            But the file /var/tmp/mydoc.png is not generated, and the messages QPainter::setFont: Painter not active and QPainter::setPen: Painter not active are displayed.

            I tried to find an example that showed QPainter and QPixmap used this way, but I failed to find.

            Does anyone have an example or reference showing how to do this?

            TIA

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @canellas
            You have to call bool QPainter::begin(QPaintDevice *device).

            Apart from that, so far as I see in code shown you never draw into painter so you're not going to see much. All you do is set a pen and never craw anything. I don't know about not saving the file, check that line is reached and check /var/tmp files are not getting deleted.

            canellasC 1 Reply Last reply
            0
            • JonBJ JonB

              @canellas
              You have to call bool QPainter::begin(QPaintDevice *device).

              Apart from that, so far as I see in code shown you never draw into painter so you're not going to see much. All you do is set a pen and never craw anything. I don't know about not saving the file, check that line is reached and check /var/tmp files are not getting deleted.

              canellasC Offline
              canellasC Offline
              canellas
              wrote on last edited by
              #5

              @JonB

              The code that actually draws was removed to reduce the size of the code.

              The line that saves the file is executed, and the files at var/tmp are not deleted by the OS automatically.

              JonBJ 2 Replies Last reply
              0
              • canellasC canellas

                @JonB

                The code that actually draws was removed to reduce the size of the code.

                The line that saves the file is executed, and the files at var/tmp are not deleted by the OS automatically.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6
                This post is deleted!
                1 Reply Last reply
                0
                • canellasC canellas

                  @JonB

                  The code that actually draws was removed to reduce the size of the code.

                  The line that saves the file is executed, and the files at var/tmp are not deleted by the OS automatically.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @canellas Start by checking return result of QPixmap::save(). I suppose it's false.

                  Is paintDevice still a null QPixmap? I think it is, depending on what code you removed. Hence earlier
                  @Christian-Ehrlicher said in Using QPainter to draw on a QPixmap and save it:

                  Your QPixmap is null. You should pass a valid QPixmap to the painter.

                  I think you have to pass a QPixmap big enough for your painting.

                  Then maybe the save() will do something. It wants something valid to save.

                  canellasC 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @canellas Start by checking return result of QPixmap::save(). I suppose it's false.

                    Is paintDevice still a null QPixmap? I think it is, depending on what code you removed. Hence earlier
                    @Christian-Ehrlicher said in Using QPainter to draw on a QPixmap and save it:

                    Your QPixmap is null. You should pass a valid QPixmap to the painter.

                    I think you have to pass a QPixmap big enough for your painting.

                    Then maybe the save() will do something. It wants something valid to save.

                    canellasC Offline
                    canellasC Offline
                    canellas
                    wrote on last edited by
                    #8

                    @JonB

                    Yes, you are correct.

                    Unlike QPdfWriter, QPixmap must be big enough for the image that is to be drawn.

                    Thanks!

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • canellasC canellas has marked this topic as solved on
                    • canellasC canellas

                      @JonB

                      Yes, you are correct.

                      Unlike QPdfWriter, QPixmap must be big enough for the image that is to be drawn.

                      Thanks!

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9
                      This post is deleted!
                      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