[Qt-interest] Making part of QWebView as transaparent

Samuel Rødal sroedal at trolltech.com
Thu Dec 17 10:26:39 CET 2009


Nitin Mahajan wrote:
> Hello,
> 
> --- On Wed, 16/12/09, Samuel Rødal <sroedal at trolltech.com> wrote:
> 
>> From: Samuel Rødal <sroedal at trolltech.com>
>> Subject: Re: [Qt-interest] Making part of QWebView as transaparent
>> To: nitinm76 at yahoo.com
>> Cc: qt-interest at trolltech.com
>> Date: Wednesday, 16 December, 2009, 2:20 PM
>> Nitin Mahajan wrote:
>>> HI,
>>>
>>> --- On Tue, 15/12/09, Samuel Rødal <sroedal at trolltech.com>
>> wrote:
>>>> From: Samuel Rødal <sroedal at trolltech.com>
>>>> Subject: Re: [Qt-interest] Making part of QWebView
>> as transaparent
>>>> To: nitinm76 at yahoo.com
>>>> Cc: qt-interest at trolltech.com
>>>> Date: Tuesday, 15 December, 2009, 6:23 PM
>>>> Nitin Mahajan wrote:
>>>>> Hello,
>>>>>
>>>>> I am trying to make a part of QWebView as
>> transparent
>>>> using setMask(). I am doing this experiment with
>> the demo
>>>> browser.
>>>>> The demo browser is allowed to run for some
>> time.Later
>>>> some region of web view is made transparent using
>> setMask().
>>>>> What I find is that the region where a whole
>> should
>>>> have been punched remains white, instead of
>> becoming
>>>> transparent. Can some one help me with this?
>>>>> regards
>>>>>
>>>>> -Nitin
>>>> Are you sure you're using setMask() on a top-level
>> widget?
>>>> Otherwise there won't be any transparency as
>> there'll be
>>>> another widget behind.
>>>>
>>> You are right. I forgot to set mask on
>> BrowserMainWindow. After doing that the transparency is
>> fine. 
>>> Now I have a new problem. The Area where I punch a
>> hole in webview,had a child widget. That widget is also not
>> being shown now. I want to show that child widget with some
>> transparency.
>>> Can I get, some pointers towards this?
>>>
>>> regards
>>>
>>> -Nitin
>> With setMask() you can only get either fully opaque or
>> fully transparent pixels. If you want semi transparency you
>> need to not use setMask() but use the window attribute
>> Qt::WA_TranslucentBackground on the top level.
>>
>> You then need to override the top level's paint event to
>> source fill the areas where you want transparency with
>> Qt::transparent, something like this:
>>
>> // first let the parent do its drawing
> Do you mean Base class's paintEvent() or Parent Widget's paint event?
>> ParentClass::paintEvent(event);
>>
>> QPainter p(this);
>> p.setCompositionMode(QPainter::CompositionMode_Source);
>>
>> // fill the region where you want transparency
> 
> Why the for loop here?

Oh, that should have been foreach (QRect rect, 
transparentRegion.rects()), in case transparentRegion is a QRegion, 
since there's no QPainter::fillRegion().

>> foreach (QRect rect, transparentRegion);
>>     p.fillRect(rect, Qt::transparent);
>>
>>
> I wrote the child widget's paint event like this, and it does most of the job the way I wanted.
> 
> void MyWidget::paintEvent(QPaintEvent *event)
> {
>      QWidget::paintEvent(event);
>      QPainter p(this);
>      p.setCompositionMode(QPainter::CompositionMode_Source);
>      p.fillRect(rect(), Qt::transparent);
> }
> 
> Only problem is my mouse, which is a PNG image appears with a very dirty square background in the transparent area. Am I missing something here?

Are you drawing the mouse pointer manually? Make sure it has a sensible 
alpha channel and that you're using CompositionMode_SourceOver and not 
CompositionMode_Source to draw it (source should only be used for the 
background transparency).

--
Samuel



More information about the Qt-interest-old mailing list