PDA

View Full Version : QDialog w/ transparent background



LarryDobson
22nd September 2006, 22:13
I know. I know.
This question has been answered 100 times.
I know. I know.
I read them all.
None of the answers work for me.
I need a dialog with a transparent client area.
All of my attempts have failed.
When I attempt to mask it out, the rest (decorations) of the dialog goes gray.
I no longer have a titlebar, frame or buttons.
Putting a transparent label on it, only shows the background of the QDialog.
This seems like it should be easy...
Please help,
TIA,
Larry

jpn
23rd September 2006, 06:56
I know. I know.
This question has been answered 100 times.
I know. I know.
I read them all.
None of the answers work for me.

Not even this (http://www.qtcentre.org/forum/f-qt-programming-2/t-qt41-transparent-widget-3033.html)? Pay attention to the parts where coordinates are being mapped from global coordinate system to widget's own coordinate system.



I need a dialog with a transparent client area.

Please be more specific. What is a transparent client area?



All of my attempts have failed.
When I attempt to mask it out, the rest (decorations) of the dialog goes gray.
I no longer have a titlebar, frame or buttons.

How are you applying the mask? Are you calculating a region? Or are you using QPixmap::mask()?



Putting a transparent label on it, only shows the background of the QDialog.

Child widgets are transparent since Qt 4.1 by default. A top level widget, however, can only be set transparent as a whole by QWidget::setWindowOpacity() (http://doc.trolltech.com/4.1/qwidget.html#windowOpacity-prop) (read notes in the docs), or parts of it can be made completely hidden by applying a mask. Note that any child widget intersecting a masked area becomes invisible too (over the masked area).

LarryDobson
25th September 2006, 16:16
Hello J-P,
I had tried your solution.
This left the caption bar and frame gray (with strange beveling).
Without the ability to move or resize.
I appreciate the help though.

"Please be more specific. What is a transparent client area?"

See geometry() // contents of dialog -vs- caption bar and frame

Very close target!
Client are is transparent, but, frame and Caption bar are gray.
Larry

LarryDobson
26th September 2006, 16:02
Got it!
Here is the solution...


void resizeEvent(QResizeEvent *e)
{
QDialog::resizeEvent(e);

// grab the geometry
QRect fg = frameGeometry();
QRect rg = geometry();

// get some reusable values
int nCaptionHeight = rg.top() - fg.top();
int nFrameWidth = rg.left() - fg.left();

// map the global coords to local for the frame
fg.moveTo(mapFromGlobal(fg.topLeft()));

// create a rectangle for the frame
QRect rectFrame(fg.left() - nFrameWidth,
fg.top() - nCaptionHeight,
fg.width() + ( nFrameWidth * 2 ),
fg.height() + ( nCaptionHeight + nFrameWidth ) );

// create a region from the frame rectangle
QRegion regionFrame( rectFrame );

// map the global coords to local for the client area
rg.moveTo(mapFromGlobal(rg.topLeft()));

// move the rect to 0,0
rg.moveTo( QPoint(0,0) );

// create a region from the client rect
QRegion regionClient( rg );

// set a mask to be the frame region minus the client region
setMask( regionFrame - regionClient );
}
You will probably recognize some of this code from this thread (http://www.qtcentre.org/forum/f-qt-programming-2/t-qt41-transparent-widget-3033.html).
Many thanks to J-P Nurmi for laying the foundation...
It just took a bit to figure out that my problem was the math (e.g. negative numbers to allow the caption bar (since QPoint(0,0) is the upper left corner of the client area).
Thanks again JPN

This is what I was looking for...:cool:

high_flyer
26th September 2006, 16:28
does this works with semi transparency as well?
EDIT: after thinking about it a bit, I think it should - so:
(in this case) QDialog gets tsetWindowOpacity() to wanted degree of opacity
The rest as in the suggested above code.
This way we get a semi opaque non square resizeable dialog.
At least in theory.

LarryDobson
26th September 2006, 18:55
does this works with semi transparency as well?
EDIT: after thinking about it a bit, I think it should - so:
(in this case) QDialog gets tsetWindowOpacity() to wanted degree of opacity
The rest as in the suggested above code.
This way we get a semi opaque non square resizeable dialog.
At least in theory.


In Theory... :eek:

"non square" ??? making the dialog semi-tranparent will not effect the corners.

wysota
26th September 2006, 19:26
"non square" ??? making the dialog semi-tranparent will not effect the corners.
No, but setMask() will.