+ Reply to Thread
Results 1 to 15 of 15

Thread: Qt4.1 Transparent Widget

  1. #1

    Default Qt4.1 Transparent Widget

    Yeah, you must think another boring thread lost in the 1000000 threads on transparent backgrounds.
    But I've spent a long time reading a lot of threads and no one is clear, simple, easy to understand for a new Qt progammer, like if there are a hundred different solutions with alpha blending, qpalette, ....
    So what I want to achieve is EXACTLY that :

    http://www.qtforum.org/attachment.php?attachmentid=798

    and if during my program the destokp background change so does the widget.
    So I don't need setWindowOpacity, I've tried all the stuff with the Palette, trying to put a blanck png pixmap on the background, nothing seems to work (or I do it bad ...)

    The solution would have been that the guys form Qt put the source code for that exemple :

    http://doc.trolltech.com/qq/qq16-background.html
    which fit my problem but they haven't.

    So anyone has an understandable solution for my problem ???

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,030
    Thanks
    3
    Thanked 4,654 Times in 4,457 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Qt4.1 Transparent Widget

    Use QWidget::setMask() to tell the widget which parts of it should be opaque and the rest will be transparent. The only difficulty is that whenever you resize the widget, the mask has to change too, so you have to attach to some event and re-set the mask after the layout does its job to reposition widgets.

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

    Default Re: Qt4.1 Transparent Widget

    Override QWidget::resizeEvent(). Here's a pseudo algo:

    Qt Code:
    1. resizeEvent
    2. {
    3. // include title and frames
    4. region = frameGeometry // frameGeometry relative to the widget (not global)
    5.  
    6. // exclude the area inside frames
    7. region -= geometry // geometry relative to the widget (not global)
    8.  
    9. // include children
    10. region += childrenRegion
    11.  
    12. // apply mask
    13. setMask region
    14. }
    To copy to clipboard, switch view to plain text mode 

    PS. The indexing feature in Qt Assistant is powerful. Try entering "keywords" used in above pseudo code..
    Attached Images Attached Images
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    djoul (18th July 2006)

  5. #4

    Default Re: Qt4.1 Transparent Widget

    My widget won't be resized, it's just a gridlayout with buttons on a backgroud. and I just want the buttons to be shown.
    I'm digging the setMask way, now I have just the border of the widget that is transparent, but my layout with the buttons are still shown, I'm trying to let the layout transparent.
    I haven' implemented the QpaintEvent as I use the setMask inthe constructor, should I use a png pixmap with a timer to call paintEvent to refresh the layout and make it transparent ?

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,030
    Thanks
    3
    Thanked 4,654 Times in 4,457 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Qt4.1 Transparent Widget

    No. Don't touch the paint event. You only need setMask() just like jpn suggested. And better put it in resize event and not in the constructor, just in case. But it should still work with the constructor (the same contents as in the resize event).

  7. #6

    Default Re: Qt4.1 Transparent Widget

    Ok, one last (stupid) question :
    With the region stuff, adding and taking off some parts, I need to know the position of my buttons to tell the mask not to take them.
    But as I use them with a Layout, that s the layout which knows where exactly they are and I don t have an idea about how knowing their coordonates, pushbutton1.x() does not exists ...

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,030
    Thanks
    3
    Thanked 4,654 Times in 4,457 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Qt4.1 Transparent Widget


  9. The following user says thank you to wysota for this useful post:

    djoul (18th July 2006)

  10. #8

    Default Re: Qt4.1 Transparent Widget

    Quote Originally Posted by jpn
    Override QWidget::resizeEvent(). Here's a pseudo algo:

    Qt Code:
    1. resizeEvent
    2. {
    3. // include title and frames
    4. region = frameGeometry // frameGeometry relative to the widget (not global)
    5.  
    6. // exclude the area inside frames
    7. region -= geometry // geometry relative to the widget (not global)
    8.  
    9. // include children
    10. region += childrenRegion
    11.  
    12. // apply mask
    13. setMask region
    14. }
    To copy to clipboard, switch view to plain text mode 

    PS. The indexing feature in Qt Assistant is powerful. Try entering "keywords" used in above pseudo code..
    I'm really a dumbass, can you give the code of the image you attached, it would be much better to understand and help me stopping yelling against my computer.
    I m too young to have white hair !!!

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,030
    Thanks
    3
    Thanked 4,654 Times in 4,457 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Qt4.1 Transparent Widget

    I think the code is more or less like this:

    Qt Code:
    1. void someWidget::resizeEvent(QResizeEvent *e){
    2. QWidget::resizeEvent(e);
    3. QRegion reg(frameGeometry());
    4. reg-=QRegion(geometry()); // this may have to be different here
    5. reg+=childrenRegion();
    6. setMask(reg);
    7. }
    To copy to clipboard, switch view to plain text mode 

    But you could have come up to that yourself, you know...

  12. #10

    Default Re: Qt4.1 Transparent Widget

    [QUOTE=wysota]I think the code is more or less like this:

    Qt Code:
    1. void someWidget::resizeEvent(QResizeEvent *e){
    2. QWidget::resizeEvent(e);
    3. QRegion reg(frameGeometry());
    4. reg-=QRegion(geometry()); // this may have to be different here
    5. reg+=childrenRegion();
    6. setMask(reg);
    7. }
    To copy to clipboard, switch view to plain text mode 
    thanks, I had problems with the childrenRegion, I thought it gave me the region for just one button, I had to apply it to several child widgets.
    The code is simple but I don t think I would have come to this quickly

    Thanks again
    Last edited by djoul; 18th July 2006 at 15:38.

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,030
    Thanks
    3
    Thanked 4,654 Times in 4,457 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Qt4.1 Transparent Widget

    This (almost, it eats up the window decoration too) works for me.
    Attached Files Attached Files

  14. The following user says thank you to wysota for this useful post:

    Lele (19th July 2006)

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

    Default Re: Qt4.1 Transparent Widget

    Quote Originally Posted by wysota
    This (almost, it eats up the window decoration too) works for me.
    It eats the window decoration because the QRect returned by QWidget::frameGeometry() and QWidget::geometry() (for a top-level widget) are relative to the global coordinate system. They must be mapped from global coordinates to the widget's own coordinate system. This is how I made it work:

    Qt Code:
    1. // map geometries to widget's coordinates
    2. QRect fg = frameGeometry();
    3. fg.moveTo(mapFromGlobal(fg.topLeft()));
    4. QRect gm = geometry();
    5. gm.moveTo(mapFromGlobal(gm.topLeft()));
    6.  
    7. // include title and frames
    8. QRegion region = fg;
    9.  
    10. // exclude the area inside frames
    11. region -= gm;
    12.  
    13. // include all children
    14. region += childrenRegion();
    15.  
    16. // apply mask
    17. setMask(region);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  16. #13
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt4.1 Transparent Widget

    I tried to have also the QPalette::Base transparent (completly or some color with alpha) but I didn't succeed, has anyone of you already tried that?
    thanks

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

    Default Re: Qt4.1 Transparent Widget

    Quote Originally Posted by Lele
    I tried to have also the QPalette::Base transparent (completly or some color with alpha) but I didn't succeed, has anyone of you already tried that?
    thanks
    It's not possible to make top level widgets transparent by just changing the palette. AFAIK there are only 2 possibilities; to make the whole window transparent by QWidget::setOpacity() or completely hide parts of the window by QWidget::setMask().
    J-P Nurmi

  18. #15
    Join Date
    Jun 2006
    Location
    Arizona
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Cool Re: Qt4.1 Transparent Widget

    Here is a related thread...
    QDialog w/ transparent background
    Larry Dobson

    If you want to be seen... stand up
    If you want to be heard... speak up
    If you want to be respected... shut up

+ Reply to Thread

Similar Threads

  1. Pin/Unpin Dock Widget
    By charlesD in forum Newbie
    Replies: 1
    Last Post: 21st June 2006, 06:57
  2. Transparent TextEdit or Widget
    By showhand in forum Qt Programming
    Replies: 8
    Last Post: 26th May 2006, 05:58
  3. Replies: 4
    Last Post: 24th March 2006, 22:50
  4. transparent widget
    By hijinks in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2006, 09:43
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

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