Results 1 to 5 of 5

Thread: Grabbing widget's background

  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Grabbing widget's background

    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.

    Qt Code:
    1. // QWidget* widget is the widget whose background we are insterested of
    2.  
    3. // map widget's rect to it's window
    4. QRect bgRect = widget->rect();
    5. bgRect.moveTo(widget->mapTo(widget->window(), bgRect.topLeft()));
    6.  
    7. // need a better solution for this to avoid flickering
    8. widget->hide();
    9. QImage behind = QPixmap::grabWidget(widget->window(), bgRect).toImage();
    10. widget->show();
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Grabbing widget's background

    Maybe you could draw appropriate image yourself using the QPalette::window() brush?

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Grabbing widget's background

    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...

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Grabbing widget's background

    Quote Originally Posted by jacek
    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..
    J-P Nurmi

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Grabbing widget's background

    Quote Originally Posted by high_flyer
    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.
    J-P Nurmi

Similar Threads

  1. Qt3 - Multiple transparent widgets
    By bythesea in forum Qt Programming
    Replies: 4
    Last Post: 11th September 2009, 11:24
  2. Upper limit on number of widgets?
    By jdiewald in forum Qt Programming
    Replies: 1
    Last Post: 29th September 2008, 23:00
  3. Replies: 2
    Last Post: 16th May 2008, 14:39
  4. widgets behind hidden widgets not working
    By bpetty in forum Newbie
    Replies: 13
    Last Post: 7th September 2007, 20:23
  5. fixing my widget's palette
    By alanh in forum Qt Programming
    Replies: 2
    Last Post: 28th February 2006, 16:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.