why QPainter cause "image composition".
-
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;
}
-
@cainiao123 said in why QPainter cause "image composition".:
QImage *mainimg = new QImage();
mainimg = QImage(240+1,240+1,QImage::Format_ARGB32); //size 240240Your 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.
-
This is the result of the program running.
-
@cainiao123 said in why QPainter cause "image composition".:
QImage *mainimg = new QImage();
mainimg = QImage(240+1,240+1,QImage::Format_ARGB32); //size 240240Your 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.
-
@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.