Results 1 to 7 of 7

Thread: QTextEdit with semi-transparent background

  1. #1
    Join Date
    Apr 2007
    Posts
    46
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTextEdit with semi-transparent background

    I have the following code to paint a semi-transparent label on an image and then write on that label:
    Qt Code:
    1. topLabel = new QTextEdit(imageLabel);
    2. // topLabel->setAttribute(Qt::WA_NoSystemBackground);
    3. QPalette palette;
    4. // a white semi-transparent background
    5. palette.setColor(QPalette::Window, QColor(255, 255, 255, 127));
    6. topLabel->setPalette(palette);
    7. topLabel->setText(*text);
    To copy to clipboard, switch view to plain text mode 
    It creates a white, opaque background.
    It topLabel is QLabel, it creates a transparent background.
    In any case, it is not semi-transparent.

  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: QTextEdit with semi-transparent background

    I have the following code to paint a semi-transparent label on an image and then write on that label
    I don't quite understand what you mean? A transparent label background?
    Why don't you do this in the paintEvent of your label? Fill it with a semitransparent pixmap, and then draw on it whatever you want...

    Regards.

  3. #3
    Join Date
    Apr 2007
    Posts
    46
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextEdit with semi-transparent background

    I mean a semi-transparent label background -- i.e. 50% white, 50% transparent.
    According to the QWidget doc, "The background can be set using ... setPalette()."
    So I'm hoping to avoid subclassing QStyle.
    The size of the background can be changed, so a semi-transparent pixmap won't work.

  4. #4
    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: QTextEdit with semi-transparent background

    You can always scale the pixmap, being transparent and all... And I didn't mean subclassing QStyle, but QLabel. I have tried this method before, and it works.

  5. #5
    Join Date
    Apr 2007
    Posts
    46
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextEdit with semi-transparent background

    Since it is only SEMI-transparent (that is, partially opaqe), scaling will give bad resolution.
    Well, true that you can use a huge-huge pixmap and scale it down, but that's not very efficient.

  6. #6
    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: QTextEdit with semi-transparent background

    Transparent pixmap, means only alpha channel, so no scaling effects occur.
    Is this what you're trying to achieve?
    Qt Code:
    1. void CTestLabel::paintEvent( QPaintEvent* )
    2. {
    3. QPainter p( this );
    4. QPixmap pix( 1, 1 );
    5. pix.fill( Qt::transparent );
    6. p.drawPixmap( rect(), pix );
    7. p.setOpacity( 0.5 );
    8. p.fillRect( rect(), Qt::white );
    9.  
    10. p.setOpacity( 1 );
    11. p.drawText( rect(), "test" );
    12. };
    To copy to clipboard, switch view to plain text mode 

    I get a white label, 50% opaque, with some text on it.

  7. The following user says thank you to marcel for this useful post:

    ber_44 (28th April 2007)

  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: QTextEdit with semi-transparent background

    Quote Originally Posted by ber_44 View Post
    Qt Code:
    1. palette.setColor(QPalette::Window, QColor(255, 255, 255, 127));
    To copy to clipboard, switch view to plain text mode 
    Shouldn't that be QPalette::Base: "Used as the background color for text entry widgets; usually white or another light color"?
    J-P Nurmi

Similar Threads

  1. transparent background of the main widget
    By nagpalma in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2007, 17:52
  2. Replies: 3
    Last Post: 8th December 2006, 18:51
  3. QDialog w/ transparent background
    By LarryDobson in forum Qt Programming
    Replies: 6
    Last Post: 26th September 2006, 19:26
  4. Transparent background Style
    By Lele in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2006, 12:02
  5. Replies: 1
    Last Post: 5th April 2006, 16:44

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.