Results 1 to 6 of 6

Thread: Drawing widgets with transparent backgrounds over a specialized widget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: Drawing widgets with transparent backgrounds over a specialized widget

    I don't have time (at the moment) to read all you have written, its just to long.
    However - if your special widget is not drawn by Qt, you wont be able to get to see it through a child widget.
    The reason for this is simple - for the transparency to work, Qt must know what is under the child widget, and if Qt didn't draw it, it can't draw what ever should be seen through the child widget.
    The problem can be often observed with QGLWidgets as well.
    If you have the information in your hand to the drawn content of your special externally drawn widget, you could pass this buffer and the correct region to the child widget - but that will be not trivial and probably not easy.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  2. #2
    Join Date
    Apr 2009
    Location
    www.JaminGrey.com
    Posts
    71
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Drawing widgets with transparent backgrounds over a specialized widget

    I'm not wanting transparency as in translucency. If I wanted something like glass (partial transparency), Qt would need to know about it. But if I'm wanting parts of a widget to be 100% transparent (i.e. entirely invisible), Qt shouldn't need that information (though the API might require it, the logical concept itself doesn't).

    I suppose I could grab the correct portion of the SFML widget, save it to a buffer, convert that buffer to a QPixmap, and draw that QPixmap as the child QWidget's background.
    That's kinda hacky programming that I'd like to avoid though, as it creates ties between the two separate classes that just shouldn't be there. I'll look into anyway, though.

    Could I get the entire buffer of the child widget, somehow, and then use that to create a mask for the widget?

  3. #3
    Join Date
    Apr 2009
    Location
    www.JaminGrey.com
    Posts
    71
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Drawing widgets with transparent backgrounds over a specialized widget

    This works:

    Qt Code:
    1. QWidget *myWidget = new QWhateverWidget();
    2. myWidget->setAutoFillBackground(false);
    3. QPixmap pixmap = QPixmap::grabWidget(myWidget);
    4. myWidget->setMask(pixmap.createHeuristicMask();
    To copy to clipboard, switch view to plain text mode 

    Easy, functional, and doesn't require custom widgets. You just need to remember to update the mask whenever the widget's layout changes.

    functioning.png

    Note: Since createHeurisiticMask() uses whatever colors are in each of the corners and chips away from there, if your widget has legitimate pixels in one of the corners they will be eaten.
    If such is the case, you might prefer createMaskFromColor() instead, using the color of the transparent pixel Qt uses. For me, this oddly appears to be QColor(173, 240, 13).
    I tried to find a constant defined somewhere with that value, but I can't locate one. Qt::transparent, and Qt::color0 / Qt::color1 don't match it.
    If QColor(173,240,13) doesn't work for you, you can instead save the pixmap (pixmap->save()) and see what color is used in your version of Qt. Obviously this method isn't future-proof however.

    [Edit:] ROFL. QColor, being lime green, I thought they were just using the color from the Qt logo (which is actually quite a bit darker). Turns out in hexadecimal it's 0xADF00D, so in ARGB (but the alpha gets lost) it's probably 0xBAADF00D, or maybe 0xDEADF00D, common debug/error values.
    Someone should submit a patch for this, and make it use Qt::transparent as the color, or create a new color for it (Qt::invisible), so it can be assured to not break in future builds.
    Or, preferablly, just return a 32 bit pixmap with proper alpha. Then you can just do: pixmap->mask().

  4. #4
    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: Drawing widgets with transparent backgrounds over a specialized widget

    But if I'm wanting parts of a widget to be 100% transparent (i.e. entirely invisible),Qt shouldn't need that information
    That is true - but I thought you wanted translucency.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Freehand drawing on transparent widget.
    By vaibhav in forum Qt Programming
    Replies: 4
    Last Post: 24th November 2010, 06:33
  2. Replies: 2
    Last Post: 31st May 2010, 11:57
  3. Replies: 0
    Last Post: 15th May 2009, 15:38
  4. Widgets on Graphicsview backgrounds
    By pherthyl in forum Qt Programming
    Replies: 4
    Last Post: 29th July 2008, 18:02
  5. Regarding drawing on Transparent Window
    By Shalabh in forum Qt Programming
    Replies: 3
    Last Post: 31st May 2007, 10:32

Tags for this Thread

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.