Results 1 to 4 of 4

Thread: How to draw opaque background with WA_TranslucentBackground active

  1. #1
    Join Date
    Aug 2020
    Posts
    15
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default How to draw opaque background with WA_TranslucentBackground active

    Edit: forgot to add - I am using Qt 5.15 under Windows 10 MSYS2 MinGW

    I have a QMainWindow subclass with
    Qt Code:
    1. setAttribute(Qt::WA_TranslucentBackground);
    To copy to clipboard, switch view to plain text mode 
    in its constructor. I want the widgets to be transparent by default, which works. But now I want to add a special widget, say the member "QWidget* m_opaque;", with a fixed non-transparent background color. However even with
    • m_opaque->setAutofillBackground(true);
    • m_opaque->setAttribute(Qt::WA_OpaquePaintEvent);
    • m_opaque->setWindowOpacity(1.0);
    • m_opaque->setStyleSheet("{ background-color: grey; }");

    the widget still has transparent background .. how do I make it opaque?
    Last edited by Maryu; 4th September 2020 at 22:25. Reason: platform/version info

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,695
    Thanks
    259
    Thanked 753 Times in 743 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to draw opaque background with WA_TranslucentBackground active

    Have you tried this?

    Qt Code:
    1. m_opaque->setAttribute( Qt::WA_TranslucentBackground, false );
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Aug 2020
    Posts
    15
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to draw opaque background with WA_TranslucentBackground active

    After trying to fix my other issue, I finally tried your answer
    Qt Code:
    1. m_opaque->setAttribute( Qt::WA_TranslucentBackground, false );
    To copy to clipboard, switch view to plain text mode 
    but this does not work either. To be specific, I am trying to show the status bar from my other question with grey brackground. Currently, if I use
    Qt Code:
    1. setAttribute(Qt::WA_TranslucentBackground);
    To copy to clipboard, switch view to plain text mode 
    in my main window constructor, nothing I do seems to be able to get rid of the transparency.

    Furthermore, I remembered that already noticed before that after show(), nothing I tried was able to change this attribute / transparency setting (and vice-versa: after show(), activating WA_TranslucentBackground does nothing). My only way out to go from transparent to non-transparent widgets in my window was to destroy the whole window and create a new one. However now I need a QLabel with transparency, and a QStatusBar without transparency in the same window ...

    I also found some bug reports about seemingly similar issues, but nothing that looked exactly the same as mine, so not sure what the status on this is.



    Edit: turns out my style sheet syntax was wrong .. the following
    Qt Code:
    1. m_opaque->->setStyleSheet("background-color: grey");
    To copy to clipboard, switch view to plain text mode 
    works. Still strange none of the other options show any affect, but at least I can use this.
    Last edited by Maryu; 5th September 2020 at 10:19. Reason: (partially) solved

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,695
    Thanks
    259
    Thanked 753 Times in 743 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to draw opaque background with WA_TranslucentBackground active

    nothing I tried was able to change this attribute / transparency setting
    Yes, I think that is correct - the attribute can only be changed when the widget has been constructed but before it is shown. Probably has something to do with the underlying window system and the way it creates the native windows wrapped by QWidget / QWindow. It is during the transition from constructing the QWidget container to showing it on screen that the underlying native window gets instantiated and configured and there are properties of the native window that can't be changed once that happens.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    Maryu (5th September 2020)

Similar Threads

  1. Replies: 4
    Last Post: 12th August 2011, 13:20
  2. Replies: 2
    Last Post: 27th June 2011, 14:44
  3. Replies: 3
    Last Post: 8th October 2010, 20:01
  4. Replies: 1
    Last Post: 25th June 2010, 19:31
  5. opacity of QLabel(background opaque)
    By Stephano in forum Qt Tools
    Replies: 1
    Last Post: 9th May 2006, 13:57

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.