Hello,
I want to render text item to image file (i.e. png), but keeping background transparent.
// some code to add item and set text
text_item->setPos(0, 0);
QRectF source_text_item_rect
= text_item
->sceneBoundingRect
();
target_text_item_rect.setX(0);
target_text_item_rect.setY(0);
target_text_item_rect.setSize(source_text_item_rect.size());
QPixmap pixmap_with_text
(qRound
(target_text_item_rect.
width()), qRound
(target_text_item_rect.
height()));
pixmap_with_text.fill(Qt::Transparent);
QPainter pixmap_painter
(&pixmap_with_text
);
graphics_scene.render(&pixmap_painter, target_text_item_rect, source_text_item_rect);
QString image_with_text_filename;
//some filename
return pixmap_with_text.save(image_with_text_filename, "PNG", 100);
QGraphicsScene graphics_scene;
QGraphicsTextItem* text_item;
// some code to add item and set text
text_item->setPos(0, 0);
QRectF source_text_item_rect = text_item->sceneBoundingRect();
QRectF target_text_item_rect;
target_text_item_rect.setX(0);
target_text_item_rect.setY(0);
target_text_item_rect.setSize(source_text_item_rect.size());
QPixmap pixmap_with_text(qRound(target_text_item_rect.width()), qRound(target_text_item_rect.height()));
pixmap_with_text.fill(Qt::Transparent);
QPainter pixmap_painter(&pixmap_with_text);
graphics_scene.render(&pixmap_painter, target_text_item_rect, source_text_item_rect);
QString image_with_text_filename; //some filename
return pixmap_with_text.save(image_with_text_filename, "PNG", 100);
To copy to clipboard, switch view to plain text mode
This produce pixmap with only transparent color and no text.
If set fill color to any (non-transparent), then it is filled with that color and with text which is over.
Is it possible to make transparent filling?
Bookmarks