PDA

View Full Version : Set transparency without setting background color



cdonts
30th September 2013, 02:42
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:


background-color: rgba(0, 0, 0, 100);

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


QFrame#myframe {background-color: rgba(0, 0, 0, 100);}

But now only the frame is getting the transparency.

Any alternatives to set transparency?

Thanks a lot.

aamer4yu
30th September 2013, 10:09
May be you can try using the Child Selector in Qt style sheets :-)

cdonts
1st October 2013, 01:35
Can you please be more specific? I need to set transparency without setting a background color. It is possible?

Thanks.

ChrisW67
1st October 2013, 04:18
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?

cdonts
3rd October 2013, 00:03
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.

wysota
3rd October 2013, 08:01
Do you want widgets to be transparent or just their background?

cdonts
4th October 2013, 21:15
Hi, thanks for the reply. Not only the background, the whole widget.

wysota
5th October 2013, 08:08
You can subclass, reimplement the paint event and use QPainter::setOpacity() before calling the base class implementation.

aamer4yu
5th October 2013, 09:12
Or you can use QWidget::setWindowOpacity on the top level window

cdonts
5th October 2013, 17:35
You can subclass, reimplement the paint event and use QPainter::setOpacity() before calling the base class implementation.


QStylePainter p(this);
p.setOpacity(0.1)M
QStyleOptionButton option;
initStyleOption(&option);
p.drawControl(QStyle::CE_PushButton, option);

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?


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.

wysota
5th October 2013, 17:52
QStylePainter p(this);
p.setOpacity(0.1)M
QStyleOptionButton option;
initStyleOption(&option);
p.drawControl(QStyle::CE_PushButton, option);

Thanks! This works.
I meant more like:


class TranslucentButton : public QPushButton {
// ...
protected:
void paintEvent(QPaintEvent *pe) {
QPainter p(this);
p.setOpacity(0.7);
QPushButton::paintEvent(pe);
}
};

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.

cdonts
5th October 2013, 18:06
I meant more like:


class TranslucentButton : public QPushButton {
// ...
protected:
void paintEvent(QPaintEvent *pe) {
QPainter p(this);
p.setOpacity(0.7);
QPushButton::paintEvent(pe);
}
};

If it works, of course...


You can also apply a QGraphicsOpacityEffect to a widget that will provide the transparency.

Fantastic! Thank you very much! :D

aamer4yu
7th October 2013, 08:43
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.

cdonts
8th October 2013, 00:50
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.