PDA

View Full Version : Transparent Window in qt4.5.1



raghvendramisra
16th June 2009, 07:08
HI all,
i have a requirement to display a transparent window such that when it is displayed i should be able to see my Desktop.

I have used setWindowOpacity() for this but it dosen't work.
Plz give me suggestion to correct my program.
Below is a small piece of code..................


TransparentWindow::TransparentWindow(QWidget *parent)
: QDialog(parent, Qt::FramelessWindowHint)
{

QPushButton *bttn = new QPushButton("Test",this);
bttn->setGeometry( QRect( 150, 100,200, 25 ) );
connect(bttn, SIGNAL(clicked()), this, SLOT(setOpaque()));

setWindowTitle(tr("Transparent Window"));
//setFixedHeight(sizeHint().height());


}
void TransparentWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QLinearGradient grad(0, 0, rect().width(), rect().height());

painter.fillRect(0, 0, rect().width(), rect().height(), grad);

}

void TransparentWindow::setOpaque()
{
setAttribute(Qt::WA_NoSystemBackground, true);
setAttribute(Qt::WA_TranslucentBackground, true);
setWindowOpacity(0.1);
repaint();

}



Regards

Raghav

wagmare
16th June 2009, 07:35
just try ..
use
void QPainter::setOpacity ( qreal opacity )


in paintEvent()
{

painter->setOpacity(opacity);
}


in void TransparentWindow::setOpaque()
{
opacity = 0.2;
update();
}

aamer4yu
16th June 2009, 08:45
Have a look at Qt::WA_TranslucentBackground
You can use it to get a transparent window with non-transparent childs.

raghvendramisra
19th June 2009, 15:36
I have tried the option QPainter::setOpacity ( qreal opacity )
Still it didn't work.

Is there any other option.

Waiting for reply.

Raghvendra

montylee
20th June 2009, 00:53
setWindowOpacity() works only if you have composite extension enabled in X11. Here's an extract from Qt documentation of setWindowOpacity():


Note that under X11 you need to have a composite manager running, and the X11 specific _NET_WM_WINDOW_OPACITY atom needs to be supported by the window manager you are using.

So, in my office PC with composite extension, setWindowOpacity() doesn't work but it works on my laptop as i have composite extension (Beryl 3D) installed.

raghvendramisra
22nd June 2009, 14:48
Thnx for that Information.

Could you plz tell me how can i enable composite extension in X11. I am new to this concept.

Plz do reply...............

Regards
raghvendra

montylee
25th October 2009, 02:19
oops, sorry i missed this thread for long :(
u can just google for "how to enable composite extension in linux or X11"

Also, if you just want to show your desktop and make your window appear as transparent, you can use the grabWindow function to get the desktop background as a QPixmap and then you can just set your window background to that pixmap.