PDA

View Full Version : QGraphicsTextItem+trasparent fill+png



nicolas1
25th November 2008, 08:52
Hello,
I want to render text item to image file (i.e. png), but keeping background transparent.


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_rec t.size());

QPixmap pixmap_with_text(qRound(target_text_item_rect.widt h()), 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);


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?

wysota
25th November 2008, 09:11
You can pass a fourth parameter specifying the value of the alpha channel while constructing a QColor object. I'm not sure why you are using GraphicsView for this, unless the example above is just a big simplification of your true code.

nicolas1
27th November 2008, 01:23
text_item->setDefaultTextColor(QColor(0, 0, 255, 255));
//

pixmap_with_text.fill(QColor(255, 255, 255, 0));



This did not help...