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?
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?