PDA

View Full Version : Grabbing widget's background



jpn
13th May 2006, 22:50
I'm doing some effects to a child widget. For performing these effects, I need to grab an image of the child widget's background. Or more correctly, grab an image of anything behind the child widget.

The problem is, that when I call QPixmap::grabWidget() for the widget's parent or window, the widget itself gets naturally drawn too. The only way I have found to avoid this is to hide the widget during the grab. This leads to a slight flickering, which is not what I want.



// QWidget* widget is the widget whose background we are insterested of

// map widget's rect to it's window
QRect bgRect = widget->rect();
bgRect.moveTo(widget->mapTo(widget->window(), bgRect.topLeft()));

// need a better solution for this to avoid flickering
widget->hide();
QImage behind = QPixmap::grabWidget(widget->window(), bgRect).toImage();
widget->show();


Any ideas, how to achieve the same without the hide/show trick, if it's even possible?

jacek
13th May 2006, 23:29
Maybe you could draw appropriate image yourself using the QPalette::window() brush?

high_flyer
14th May 2006, 00:19
Just an idea, not sure if it will work, but worth a try:
Try grabbing the desktop (QApplication::desktop()), and crop the grabbed image with the global coordinates of your child widget...

jpn
14th May 2006, 07:42
Maybe you could draw appropriate image yourself using the QPalette::window() brush?
A background brush alone won't do the trick because I wan't an exact image of how would it look like in place if there was no that specific widget. The widget is not necessarily a direct descendant of the window, there might be other widgets in between.

The situation could look for example like this:


------------------
| Window........ |
| -------------- |
| | Widget A.. | |
| | ---------- | |
| | |Widget B| | |
| | ---------- | |
| -------------- |
------------------

... and I would be interested of Widget B's background. QPixmap::grabWidget() draws a widget and all of it's children, recursively. That's why I need to find out a trick how to prevent Widget B from drawing itself during the time the window is grabbed. The hiding method is the only way I've found so far, but unfortunately leads to a slight flicker as I mentioned..

jpn
14th May 2006, 07:58
Just an idea, not sure if it will work, but worth a try:
Try grabbing the desktop (QApplication::desktop()), and crop the grabbed image with the global coordinates of your child widget...
Thanks for the tip, but I would still have to hide the child widget during the grab.
I was hoping for something like a widget attribute or so, with which could I temporarily prevent the widget from drawing itself.