Results 1 to 14 of 14

Thread: Set transparency without setting background color

  1. #1
    Join Date
    Aug 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Set transparency without setting background color

    Hi. I want to set some transparency to a frame and all it child widgets, but without setting a background color. The unique method I know to set transparency is something like:

    Qt Code:
    1. background-color: rgba(0, 0, 0, 100);
    To copy to clipboard, switch view to plain text mode 

    But the background color will be apllied to all child widgets. So, I can do something like:

    Qt Code:
    1. QFrame#myframe {background-color: rgba(0, 0, 0, 100);}
    To copy to clipboard, switch view to plain text mode 

    But now only the frame is getting the transparency.

    Any alternatives to set transparency?

    Thanks a lot.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Set transparency without setting background color

    May be you can try using the Child Selector in Qt style sheets :-)

  3. #3
    Join Date
    Aug 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set transparency without setting background color

    Can you please be more specific? I need to set transparency without setting a background color. It is possible?

    Thanks.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Set transparency without setting background color

    So your complaint is:
    If I apply a transparent black background to all widgets, all widgets get a transparent black background.
    If I apply a transparent black background only to a specific widget only that specific widget gets the transparent black background.
    Both of these are expected behaviour.

    What exactly is the problem again?

  5. #5
    Join Date
    Aug 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set transparency without setting background color

    I wonder if is there any way to set transparency to a specific widget or all widgets without setting the background colour. For example, if I have label1 with a red background, label2 with a blue background and label3 with a green background. I don't know which color they have, but I want to set them some transparency. The only way I know is to do "background-color: rgba(0, 0, 0, 100);", but those widgets will loose their original color. Thanks.

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

    Default Re: Set transparency without setting background color

    Do you want widgets to be transparent or just their background?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set transparency without setting background color

    Hi, thanks for the reply. Not only the background, the whole widget.

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

    Default Re: Set transparency without setting background color

    You can subclass, reimplement the paint event and use QPainter::setOpacity() before calling the base class implementation.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Set transparency without setting background color

    Or you can use QWidget::setWindowOpacity on the top level window

  10. #10
    Join Date
    Aug 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set transparency without setting background color

    Quote Originally Posted by wysota View Post
    You can subclass, reimplement the paint event and use QPainter::setOpacity() before calling the base class implementation.
    Qt Code:
    1. QStylePainter p(this);
    2. p.setOpacity(0.1)M
    3. initStyleOption(&option);
    4. p.drawControl(QStyle::CE_PushButton, option);
    To copy to clipboard, switch view to plain text mode 

    Thanks! This works. But, that is the unique way to set transparency to a widget? Each widget I have to set some transparency I need to subclass it and replace the paintEvent function?

    Quote Originally Posted by aamer4yu View Post
    Or you can use QWidget::setWindowOpacity on the top level window
    That's for the window or for any widget? It didn't work for me. Thanks.

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

    Default Re: Set transparency without setting background color

    Quote Originally Posted by cdonts View Post
    Qt Code:
    1. QStylePainter p(this);
    2. p.setOpacity(0.1)M
    3. initStyleOption(&option);
    4. p.drawControl(QStyle::CE_PushButton, option);
    To copy to clipboard, switch view to plain text mode 

    Thanks! This works.
    I meant more like:

    Qt Code:
    1. class TranslucentButton : public QPushButton {
    2. // ...
    3. protected:
    4. void paintEvent(QPaintEvent *pe) {
    5. QPainter p(this);
    6. p.setOpacity(0.7);
    7. QPushButton::paintEvent(pe);
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 

    If it works, of course...

    But, that is the unique way to set transparency to a widget?
    You can also apply a QGraphicsOpacityEffect to a widget that will provide the transparency.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Aug 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set transparency without setting background color

    Quote Originally Posted by wysota View Post
    I meant more like:

    Qt Code:
    1. class TranslucentButton : public QPushButton {
    2. // ...
    3. protected:
    4. void paintEvent(QPaintEvent *pe) {
    5. QPainter p(this);
    6. p.setOpacity(0.7);
    7. QPushButton::paintEvent(pe);
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 

    If it works, of course...


    You can also apply a QGraphicsOpacityEffect to a widget that will provide the transparency.
    Fantastic! Thank you very much!

  13. #13
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Set transparency without setting background color

    Quote Originally Posted by cdonts View Post
    That's for the window or for any widget? It didn't work for me. Thanks.
    It is for top level window.
    in main.cpp, just use setWindowOpacity() on the top level window you create.

  14. #14
    Join Date
    Aug 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set transparency without setting background color

    Quote Originally Posted by aamer4yu View Post
    It is for top level window.
    in main.cpp, just use setWindowOpacity() on the top level window you create.
    I'm not using C++. It works. Thank you.

Similar Threads

  1. Less cost by setting QWidget's background color?
    By cic in forum Qt Programming
    Replies: 1
    Last Post: 11th September 2013, 10:24
  2. Setting the background color of a header in TableView
    By sunilqt in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2013, 13:06
  3. Setting Background Color
    By SixDegrees in forum Qt Programming
    Replies: 5
    Last Post: 16th February 2011, 18:33
  4. setting background color and repositioning
    By mind_freak in forum Qt Programming
    Replies: 2
    Last Post: 11th August 2009, 06:22
  5. setting background color of QMessageBox
    By wagmare in forum Qt Programming
    Replies: 7
    Last Post: 23rd May 2009, 13:26

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
  •  
Qt is a trademark of The Qt Company.