Results 1 to 18 of 18

Thread: How to set Qt window transparent?

  1. #1
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How to set Qt window transparent?

    I have an Qt application which is executed from a web page. The Qt window comes on top of the web page and it needs to be transparent. So, the background on the web page should be visible in Qt window. Also, i need the transparency to be configurable so user can specify the percentage of transparency.

    Now, i am able to achieve transparency by setting the Qt window background image to be same as the background of the web page and using style sheets to achieve transparency. So, essentially i am not making the main Qt window transparent. I am making the widgets inside the window transparent.

    I am using the following code in the contructor of my application:
    Qt Code:
    1. QPalette palette = this->palette();
    2. palette.setBrush(backgroundRole(), QBrush(backPixmap));
    3. setPalette(palette);
    To copy to clipboard, switch view to plain text mode 
    Here backPixmap contains the background png image. So, initially i set the background image for the Qt application.

    Mainly, i have a QTableView in my Qt application, so i am setting it transparent by using the following style sheet syntax:

    Qt Code:
    1. color: rgb(212, 248, 255); background-color: rgba(230, 230, 230, 50%); selection-color: rgb(26, 26, 26); selection-background-color: rgb(143, 237, 255)
    2. }
    To copy to clipboard, switch view to plain text mode 

    So, user can change the alpha value in the background-color attribute and change the amount of transparency.


    The problem with the above approch is that if the Qt application is opened from a web page with different background, it won't look as transparent, so i want to make the window itself transparent.

    I tried using various methods of making window transparent but none succedded. For e.g. i tried this:

    Qt Code:
    1. QPalette pal = this->palette();
    2. pal.setBrush(QPalette::Base, Qt::transparent);
    3. this->setPalette(pal);
    To copy to clipboard, switch view to plain text mode 

    and also this:
    Qt Code:
    1. setStyleSheet("background-color: rgba(255, 255, 0, 50%)");
    To copy to clipboard, switch view to plain text mode 

    and this:
    Qt Code:
    1. setAttribute( Qt::WA_NoSystemBackground, true );
    To copy to clipboard, switch view to plain text mode 

    When i try the above methods, the window background changes to black. Through the stylesheet code given above, if i set the alpha value to > 10%, it appears that alpha value is making a difference but when i set it to 0%, the window goes black.

    I also tried this code given by @Jpn in some other thread:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MaskedLabel : public QLabel
    4. {
    5. protected:
    6. void resizeEvent(QResizeEvent* event)
    7. {
    8. QLabel::resizeEvent(event);
    9.  
    10. QPixmap pixmap(size());
    11. pixmap.fill(Qt::transparent);
    12. QPainter::setRedirected(this, &pixmap);
    13. QPaintEvent pe(rect());
    14. paintEvent(&pe);
    15. QPainter::restoreRedirected(this);
    16. setMask(pixmap.mask());
    17. }
    18. };
    19.  
    20. int main(int argc, char* argv[])
    21. {
    22. QApplication a(argc, argv);
    23. QLabel* label = new MaskedLabel();
    24. label->setText("Qt Centre!");
    25. QFont font = label->font();
    26. font.setPointSize(72);
    27. label->setFont(font);
    28. label->show();
    29. return a.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

    but this makes the entire window transparent and even the child widgets of the window are not visible.

    So, how can i make my window transparent and also vary the transparency percentage?

  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: How to set Qt window transparent?

    What are you using to display web page ?

    It is possible to show variable transparency for child widgets with opaque main window.

    But it is difficult to have variable transparency main window and and variable transparency child widgets.

    If your case is first one, you can use child widgets with transparent background. You dont need to go the mask way. The mask method is for achieving the second case. ie, to draw/write on desktop.

  3. #3
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to set Qt window transparent?

    The web page is shown in Opera browser and it calls the Qt application through a CGI script.

    I also want to know how can i open a fixed size frameless Qt application from a web page. Should i use QtWebView? or should i just set the window to be frameless?

    Actually as i wrote earlier i am already using the 1st method i.e. variable transparency child widgets using style sheets.

    I want to make the main window transparent and the child widgets can be fully transparent. By varying the transparency of the main window i can get my desired effect. So in that case i don't need variable transparency for the child widgets. I just need to make the main window transparent and be able to vary it's transparency.

  4. #4
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to set Qt window transparent?

    You may want to look at QWidget::setOpacity(), although this won't work on all systems and video drivers.

  5. #5
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to set Qt window transparent?

    Quote Originally Posted by Brandybuck View Post
    You may want to look at QWidget::setOpacity(), although this won't work on all systems and video drivers.
    i just checked in the Qt documentation and setOpacity function doesn't not belong to the QWidget class. It's part of QPainter class.
    The API i tried was QWidget::setWindowOpacity but it does nothing in my case.

  6. #6
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to set Qt window transparent?

    can anybody help me with this? is there no way to make a Qt window transparent and allow variable transparency?

  7. #7
    Join Date
    Jan 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set Qt window transparent?

    Did you enable compisite extension on you X configuration ?

    Maybe this helps you:

    http://marcocelentano.blogspot.com/

    Cu,
    Nando

  8. #8
    Join Date
    Jan 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to set Qt window transparent?

    Did you enable composite extensions on your X configuration ?

    Maybe this helps you:

    http://marcocelentano.blogspot.com/

    Cu,
    Nando

  9. #9
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to set Qt window transparent?

    thanks nando! i'll try this and let you know.

  10. #10
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to set Qt window transparent?

    Nando, i tried the settings given on ur blog in xorg.conf file but it didn't make a difference.
    Then i installed 3D drivers for my ATI 3650 card and enabled Compiz effects and then i was able to set the translucency thru setWindowOpacity.

    Now, the question is that how does enabling compiz effects turn on the translucency? i wanna know which file i need to modify. I need to run my application on an embedded machine with embedded linux on X11, so i need to know the file name.

    I'll try the xorg.conf settings on the embedded machine tomorrow.

  11. #11
    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: How to set Qt window transparent?

    Forget about transculency on embedded linux. Your X server there probably doesn't support compositing anyway. I'm going to repeat myself one more time - implement your application as a plugin to the webbrowser and switch the browser to WebKit or make everything (including the browser itself) a single application.

  12. #12
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to set Qt window transparent?

    Quote Originally Posted by wysota View Post
    Forget about transculency on embedded linux. Your X server there probably doesn't support compositing anyway. I'm going to repeat myself one more time - implement your application as a plugin to the webbrowser and switch the browser to WebKit or make everything (including the browser itself) a single application.
    so it is not possible to get translucency on embedded linux anyway? A normal X11R7 server is running on the embedded hardware.

    About the webkit thing, you are correct but i can't force the company to change it. I am temporary guy working for that company and i have been given a module which i am working on, so i really can't force them to do this as the project schedule is tight and they are using the browser for other applications as well.

  13. #13
    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: How to set Qt window transparent?

    Quote Originally Posted by montylee View Post
    so it is not possible to get translucency on embedded linux anyway? A normal X11R7 server is running on the embedded hardware.
    Even if you manage to enable compositions in the server, they will probably be done in software which will be hell slow.

    About the webkit thing, you are correct but i can't force the company to change it. I am temporary guy working for that company and i have been given a module which i am working on, so i really can't force them to do this as the project schedule is tight and they are using the browser for other applications as well.
    If you tell them it is not possible to obtain the effect they want, maybe this will give them something to think about. Currently I suggest you stick with rendering the browser's window to a pixmap and use that as a background of your widget. It is unlikely you are going to get anything better.

  14. #14
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to set Qt window transparent?

    Hmmm thanks for the suggestions!
    I'll try to enable transparency today in the embedded hardware and if it doesn't work i'll use pixmap as the widget background.

    But i still don't know which file does compiz modify to enable X11 composite extensions.

  15. #15
    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: How to set Qt window transparent?

    /etc/X11/xorg.conf should contain an entry such as the following:
    txt Code:
    1. Section "Extensions"
    2. Option "Composite" "Enable"
    3. EndSection
    To copy to clipboard, switch view to plain text mode 

    Also check glxinfo|grep rendering to see if it has direct rendering available.

  16. #16
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to set Qt window transparent?

    i tried running the code on the embedded hardware but couldn't see any translucency. I'll see if it has direct rendering.

  17. #17
    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: How to set Qt window transparent?

    It is possible you need to turn on a compositing manager. Check out xcompmgr.

  18. #18
    Join Date
    Dec 2013
    Posts
    1
    Qt products
    Platforms
    Windows

    Default Re: How to set Qt window transparent?

    Quote Originally Posted by aamer4yu View Post
    It is possible to show variable transparency for child widgets with opaque main window.
    Could you provide a reference on how to do that?

Similar Threads

  1. how QTextEdit transparent to his parent window?
    By wesley in forum Qt Programming
    Replies: 4
    Last Post: 16th September 2009, 08:23
  2. Transparent window with QGLWidget
    By ultr in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2008, 08:01
  3. Set a window as child at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2007, 10:30
  4. Regarding drawing on Transparent Window
    By Shalabh in forum Qt Programming
    Replies: 3
    Last Post: 31st May 2007, 11:32
  5. Making some part of window transparent
    By yogeshm02 in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2006, 21:36

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.