PDA

View Full Version : How to set Qt window transparent?



montylee
16th January 2009, 00:05
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:

QPalette palette = this->palette();
palette.setBrush(backgroundRole(), QBrush(backPixmap));
setPalette(palette);
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:


QTableView {
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)
}

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:


QPalette pal = this->palette();
pal.setBrush(QPalette::Base, Qt::transparent);
this->setPalette(pal);

and also this:

setStyleSheet("background-color: rgba(255, 255, 0, 50%)");

and this:

setAttribute( Qt::WA_NoSystemBackground, true );

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:

#include <QtGui>

class MaskedLabel : public QLabel
{
protected:
void resizeEvent(QResizeEvent* event)
{
QLabel::resizeEvent(event);

QPixmap pixmap(size());
pixmap.fill(Qt::transparent);
QPainter::setRedirected(this, &pixmap);
QPaintEvent pe(rect());
paintEvent(&pe);
QPainter::restoreRedirected(this);
setMask(pixmap.mask());
}
};

int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QLabel* label = new MaskedLabel();
label->setText("Qt Centre!");
QFont font = label->font();
font.setPointSize(72);
label->setFont(font);
label->show();
return a.exec();
}

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?

aamer4yu
16th January 2009, 04:32
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.

montylee
16th January 2009, 15:48
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.

Brandybuck
16th January 2009, 19:49
You may want to look at QWidget::setOpacity(), although this won't work on all systems and video drivers.

montylee
16th January 2009, 21:58
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.

montylee
17th January 2009, 22:55
can anybody help me with this? is there no way to make a Qt window transparent and allow variable transparency?

nando76
17th January 2009, 23:00
Did you enable compisite extension on you X configuration ?

Maybe this helps you:

http://marcocelentano.blogspot.com/

Cu,
Nando

nando76
17th January 2009, 23:01
Did you enable composite extensions on your X configuration ?

Maybe this helps you:

http://marcocelentano.blogspot.com/

Cu,
Nando

montylee
18th January 2009, 09:24
thanks nando! i'll try this and let you know.

montylee
22nd January 2009, 05:05
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.

wysota
22nd January 2009, 07:25
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.

montylee
22nd January 2009, 07:37
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.

wysota
22nd January 2009, 08:21
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.

montylee
22nd January 2009, 15:37
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.

wysota
22nd January 2009, 19:46
/etc/X11/xorg.conf should contain an entry such as the following:
Section "Extensions"
Option "Composite" "Enable"
EndSection

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

montylee
23rd January 2009, 03:17
i tried running the code on the embedded hardware but couldn't see any translucency. I'll see if it has direct rendering.

wysota
23rd January 2009, 09:00
It is possible you need to turn on a compositing manager. Check out xcompmgr.

DanielSank
24th December 2013, 20:11
It is possible to show variable transparency for child widgets with opaque main window.

Could you provide a reference on how to do that?