PDA

View Full Version : QRubberBand widget background transparent



bmn
29th March 2016, 17:05
Hello,

I am trying to get rid of the half-transparent fill of the QRubberBand widget. I just want it transparent.
What I already tried:


QPalette p = _rubberBand->palette();
p.setColor(QPalette::Background, Qt::transparent);
_rubberBand->setPalette(p);
and

_rubberBand->setStyleSheet("background-color: rgba(0,0,0,0)");
and

_rubberBand->setPalette(Qt::transparent);
_rubberBand->setAttribute(Qt::WA_NoSystemBackground);
_rubberBand->setAttribute(Qt::WA_TranslucentBackground);
_rubberBand->setAttribute(Qt::WA_TransparentForMouseEvents);

but there is no effect at all. The rectangle is always rendered the same way (see attachment)...

How can I fix this?

Thank you in advance.

bmn
30th March 2016, 11:19
the way how it is rendered seems to be platform dependant, since there is no semi-transparent background when I compile/run the application on Linux or MAC.

bmn
5th April 2016, 17:03
nobody got an idea?

d_stranz
5th April 2016, 20:59
By default a rectangular rubber band (s is Rectangle) will use a mask, so that a small border of the rectangle is all that is visible. Some styles (e.g., native Mac OS X) will change this and call QWidget::setWindowOpacity() to make a semi-transparent filled selection rectangle.

Try calling setWindowOpacity( 0.0 ) after you construct the rubber band.

bmn
7th April 2016, 09:12
Thank you for this tip, but it also has no effect at all.

d_stranz
7th April 2016, 16:17
Sounds like you are being defeated either by the OS or a style setting. Maybe try deriving from QRubberBand, overriding the paintEvent() and messing with the style option there before passing it to the base class for the actual painting.

jules1337
23rd April 2016, 22:40
For Android its a littlebit weird.
But the Answer is simple.

Subclass what you want...

//constructor
setWindowFlags(Qt::FramelessWindowHint); <--- In the constructor

//paintEvent
//IF YOU WANT IT SEMI TRANSPARENT ...
//If you want it transparent only, you dont have to overide paintEvent. Then its enough to use Qt::FramelessWindowHint in a subclass. But a subclass is not needed

void Example_Dialog:: paintEvent(QPaintEvent *)
{
QColor backgroundColor = palette().background().color();
backgroundColor.setAlpha(216); // Use Alphachannel u want
QPainter painter(this);
painter.fillRect(rect(),backgroundColor);
}

Dont tested it with QRubberband, but I think it will work with all Object the usage of setWindowFlags is possible.
Just set Qpalette with the customized Color dont work

In Windows:
setWindowOpacity(0.8);

jules1337
3rd May 2016, 17:39
In some cases you need to set the original windowFlags and add Framelesswindowhint.

I tested this with QMenu. Didnt work. But Framelesswindowhint is an important part too.

For QMenu u have to set Framelesswindowhint and the old windowFlags. But have to delete the Popup windowflag on Android.
You have to do 3 steps to immitate theese flags.
On Android you have to set Framelesswindowhint and the old windowFlags But have to delete the Popup windowflag. Then you just have to edit the Stylesheet with an transparent or semitransparent background. No need for an paintevent.

On Windows you just have to add the framelesswindowint , setAttribute(Qt::WA_TranslucentBackground) and have to set the stylesheet too.