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. why QPainter cause "image composition".
Qt 6.11 is out! See what's new in the release blog

why QPainter cause "image composition".

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 374 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.
  • C Offline
    C Offline
    cainiao123
    wrote on last edited by
    #1

    I use the method "QPrinter.drawText( )" to genarate a image, and set it in a lable.
    If i genarate several times, it will cause "image composition".
    I don't user the QPrinter.setCompositionMode(xxx), how it could be? below is the codes , please help me !

    void MainWindow::on_pushButton_clicked()
    {
    static int i=0;

    QImage *mainimg = new QImage();
    *mainimg = QImage(240+1,240+1,QImage::Format_ARGB32); //size 240*240
    QPainter painter(mainimg);
    
    painter.setPen(QColor(Qt::black));
    painter.drawRect(0,0,240,240);
    painter.drawLine(0,120,240,120);
    
    QFont f12(u8"宋体",12);
    painter.setFont(f12);
    painter.drawText(10,20,"测试重复生成图片");
    
    char text[100];
    sprintf(text,"%d%d%d%d%d",i,i,i,i,i);
    painter.drawText(10,50,text);
    qDebug()<<i++<<":"<<text;
    painter.end();
    
    QPixmap map = QPixmap::fromImage(*mainimg);
    ui->label_test->setPixmap(map);
    
    delete mainimg;
    

    }

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @cainiao123 said in why QPainter cause "image composition".:

      QImage *mainimg = new QImage();
      mainimg = QImage(240+1,240+1,QImage::Format_ARGB32); //size 240240

      Your create a memleaks here for no reason - create the QImage on the stack.

      Also you don't initialize your QImage with a background color so everywhere where you don't paint anything random stuff will be shown (for instance your old data). Clear your QImage with QPainter::fillRect() or similar.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      C 1 Reply Last reply
      1
      • C Offline
        C Offline
        cainiao123
        wrote on last edited by
        #2

        This is the result of the program running.
        788a234a-25e6-41ba-a80d-df9757488bef-image.png

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @cainiao123 said in why QPainter cause "image composition".:

          QImage *mainimg = new QImage();
          mainimg = QImage(240+1,240+1,QImage::Format_ARGB32); //size 240240

          Your create a memleaks here for no reason - create the QImage on the stack.

          Also you don't initialize your QImage with a background color so everywhere where you don't paint anything random stuff will be shown (for instance your old data). Clear your QImage with QPainter::fillRect() or similar.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          C 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @cainiao123 said in why QPainter cause "image composition".:

            QImage *mainimg = new QImage();
            mainimg = QImage(240+1,240+1,QImage::Format_ARGB32); //size 240240

            Your create a memleaks here for no reason - create the QImage on the stack.

            Also you don't initialize your QImage with a background color so everywhere where you don't paint anything random stuff will be shown (for instance your old data). Clear your QImage with QPainter::fillRect() or similar.

            C Offline
            C Offline
            cainiao123
            wrote on last edited by
            #4

            @Christian-Ehrlicher thank you very much
            I set the background color to the QPainter and make a lot of test,
            It seems to be no problem now~
            thank you again.

            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