Results 1 to 7 of 7

Thread: Changing Opacity in Qlabel

  1. #1
    Join Date
    Jul 2006
    Posts
    37
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question Changing Opacity in Qlabel

    Hi Everybody,

    I try to change the opacity in QLabel.
    Using the paintEvent with this kind of code:

    Qt Code:
    1. void myLabel::paintEvent(QPaintEvent * e)
    2. {
    3.  
    4. QPainter p(this);
    5.  
    6. // if pixmap
    7. if (! pixmap()->isNull())
    8. p.drawPixmap(pixmap()->rect(), * pixmap());
    9.  
    10. p.setOpacity(0.5);
    11.  
    12. // if Text
    13. if (! text().isEmpty())
    14. p.drawText(rect(), text());
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    The aim is just to change the opacity level ; The problem with this code is that , I have to redraw everything manually : style , pixmap position, text position...
    it's not so easy...

    If someone has something more easy, it will be fine

    Thanks to all

    David

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Changing Opacity in Qlabel

    You can call the base class paintEvent in your paint event and then just draw all additional stuff.

  3. #3
    Join Date
    Jul 2006
    Posts
    37
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Changing Opacity in Qlabel

    But the problem is :

    opacity required :Qpainter

    On my label I apply a style sheet, a pixmap, and sometimes a text.

    When I do
    Qt Code:
    1. QPainter p(this);
    To copy to clipboard, switch view to plain text mode 
    eveything must be redraw

    and I don't know How to change the opacity with the class QPaintEvent()

    Thanks

    David

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Changing Opacity in Qlabel

    For a transparent pixmap, apply the opacity to the pixmap:

    Qt Code:
    1. QPixmap original = ...;
    2.  
    3. QPixmap result(original.size());
    4. result.fill(Qt::transparent);
    5. QPainter painter;
    6. painter.setOpacity(0.5);
    7. painter.begin(&result);
    8. painter.drawPixmap(0, 0, original);
    9. painter.end();
    10.  
    11. label->setPixmap(result);
    To copy to clipboard, switch view to plain text mode 

    For transparent text, adjust palette:
    Qt Code:
    1. QPalette palette = label->palette();
    2. QColor color = palette.color(QPalette::Text);
    3. color.setAlpha(127);
    4. palette.setColor(QPalette::Text, color);
    5. label->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 
    or use style sheets:
    Qt Code:
    1. QLabel { color: rgba(0, 0, 0, 50%) }
    To copy to clipboard, switch view to plain text mode 
    No reimplementing of paintEvent() needed.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    montylee (20th October 2009)

  6. #5
    Join Date
    Jul 2006
    Posts
    37
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Changing Opacity in Qlabel

    I 've tested your exmaple but its didn't work

    This is what I've done in a subclass of a label:


    Qt Code:
    1. void myLabel::setPixmap(const QPixmap & pix)
    2. {
    3.  
    4. QPixmap result(pix);
    5. result.fill(Qt::transparent);
    6.  
    7. QPainter painter;
    8. painter.setOpacity(0.5);
    9. painter.begin(&result);
    10. painter.drawPixmap(0, 0, result);
    11. painter.end();
    12.  
    13. QLabel::setPixmap(result);
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    May I do something wrong ; the pixmap is not visible for me

    Thanks

  7. #6
    Join Date
    Jul 2006
    Posts
    37
    Thanks
    6
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Changing Opacity in Qlabel

    after many test : I found my (stupid) problem

    the good code on a subclass of QLabel:

    Qt Code:
    1. QPixmap result(pix.size());
    2. result.fill(Qt::transparent);
    3.  
    4. QPainter painter;
    5. painter.begin(&result);
    6. painter.setOpacity(0.5);
    7. painter.drawPixmap(0, 0, pix);
    8. painter.end();
    9.  
    10. QLabel::setPixmap(result);
    To copy to clipboard, switch view to plain text mode 

    opacity must be done afer the "begin"


    Ok thanks to all

    David

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Changing Opacity in Qlabel

    Quote Originally Posted by desch View Post
    after many test : I found my (stupid) problem

    opacity must be done afer the "begin"
    Sorry, that's actually my bad.
    J-P Nurmi

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.