Results 1 to 3 of 3

Thread: QGraphicsTextItem+trasparent fill+png

  1. #1
    Join Date
    Jun 2008
    Posts
    35
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsTextItem+trasparent fill+png

    Hello,
    I want to render text item to image file (i.e. png), but keeping background transparent.
    Qt Code:
    1. QGraphicsScene graphics_scene;
    2.  
    3. QGraphicsTextItem* text_item;
    4.  
    5. // some code to add item and set text
    6.  
    7. text_item->setPos(0, 0);
    8.  
    9. QRectF source_text_item_rect = text_item->sceneBoundingRect();
    10.  
    11. QRectF target_text_item_rect;
    12.  
    13. target_text_item_rect.setX(0);
    14. target_text_item_rect.setY(0);
    15. target_text_item_rect.setSize(source_text_item_rect.size());
    16.  
    17. QPixmap pixmap_with_text(qRound(target_text_item_rect.width()), qRound(target_text_item_rect.height()));
    18.  
    19. pixmap_with_text.fill(Qt::Transparent);
    20.  
    21. QPainter pixmap_painter(&pixmap_with_text);
    22.  
    23. graphics_scene.render(&pixmap_painter, target_text_item_rect, source_text_item_rect);
    24.  
    25. QString image_with_text_filename; //some filename
    26.  
    27. 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?
    Last edited by wysota; 25th November 2008 at 08:10.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsTextItem+trasparent fill+png

    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.

  3. #3
    Join Date
    Jun 2008
    Posts
    35
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsTextItem+trasparent fill+png

    Qt Code:
    1. text_item->setDefaultTextColor(QColor(0, 0, 255, 255));
    2. //
    3.  
    4. pixmap_with_text.fill(QColor(255, 255, 255, 0));
    To copy to clipboard, switch view to plain text mode 

    This did not help...
    Last edited by nicolas1; 27th November 2008 at 00:35.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.