Results 1 to 8 of 8

Thread: Transparency with QWinWidget

  1. #1
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Transparency with QWinWidget

    Hi,

    I'm trying to make a QWinWidget transparent over the underlying win32 app, so that I can use Qt's QGraphicsView and all the other great UI classes within my application without blocking the underlying apps controls and graphics.

    So heres some example code of what I'm trying to do:

    Here I have my dll with all the Qt code that I need.

    Qt Code:
    1. BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID lpvReserved )
    2. {
    3. static bool ownApplication = FALSE;
    4.  
    5. if ( dwReason == DLL_PROCESS_ATTACH )
    6. ownApplication = QMfcApp::pluginInstance( hInstance );
    7. if ( dwReason == DLL_PROCESS_DETACH && ownApplication )
    8. delete qApp;
    9.  
    10. return TRUE;
    11. }
    12.  
    13.  
    14. extern "C" __declspec(dllexport) bool initGUI( HWND parent )
    15. {
    16. QWinWidget* win = new QWinWidget( parent );
    17. win->setFixedSize(600,200);
    18. win->move(10,10);
    19.  
    20. QHBoxLayout *hbox = new QHBoxLayout(win);
    21.  
    22. QLabel *label = new QLabel("QWinWidget -> QLabel");
    23. QLineEdit *edit = new QLineEdit();
    24.  
    25. hbox->addWidget(label);
    26. hbox->addWidget(edit);
    27.  
    28. win->show();
    29.  
    30. return true;
    31. }
    To copy to clipboard, switch view to plain text mode 

    initGUI is called from within WinMain of the win32 application which creates a standard black window using CreateWindowEx.

    Qt Code:
    1. HMODULE mod = LoadLibrary(L"MigrateApplication.dll");
    2.  
    3. typedef BOOL(*pInitGUI)(HWND parent);
    4. pInitGUI showGUI = (pInitGUI)GetProcAddress( mod, "initGUI" );
    5. if ( showGUI )
    6. showGUI( hWnd );
    To copy to clipboard, switch view to plain text mode 

    Now if you look at the attached picture, you will see the QWinWidget's beige background covers the black background of the application. I would like it so that for the Qt Widgets only the QLabel and QLineEdit are viewable and the QWinWidget's background is transparent.

    I know Qt::WA_TranslucentBackground can be used for this type of thing, but that requires a frameless window and I need the widget's embedded in the main window.

    I may be crazy but I'm looking for any and all possible ways to achieve this, thanks for reading and for any help!

    This is for Qt 4.5+, Windows. I have attached the sample applications source.
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Transparency with QWinWidget

    Did you try QWidget::setMask()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    martian (30th June 2009)

  4. #3
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Transparency with QWinWidget

    Awesome! Thank you! I had played with it a bit, but your post gave me the confidence to keep trying down that path and I got it working Hopefully performance won't be too much of an issue.

    My next problem is that when drawing text the masking combined with the font's anti-aliasing makes them look quite bad. Any way to have them anti-aliased with a transparent color or the font's main color? Currently the silhouetted edges are using the widgets background for anti-aliasing I believe, creating a white silhouette.

    See screen-shots below. The gray is the background of the win32 application.
    Attached Images Attached Images

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Transparency with QWinWidget

    Did you try just setting the background role of the widget's palette to Qt::transparent instead of setting the mask?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    martian (30th June 2009)

  7. #5
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Transparency with QWinWidget

    Yip I've tried that, just sets the widget's background to black as opposed to its standard gray.

    Qt Code:
    1. QPalette p(win->palette());
    2. p.setColor(QPalette::Background, Qt::transparent);
    3. win->setPalette(p);
    To copy to clipboard, switch view to plain text mode 

    The setMask function is working okay for my purposes so far, just need to solve the ugly font rendering.
    Attached Images Attached Images

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Transparency with QWinWidget

    It seems that QWinWidget uses some dirty hack to compose the widgets and therefore I don't think you will be able to obtain transparency in the Qt widget. You might try using the translucent background attribute after all, maybe it helps somewhat but apart from that setMask is all you can use.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #7
    Join Date
    Jun 2009
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Transparency with QWinWidget

    setMask is working fine, so I will just look into how Qt does the anti-aliasing on fonts and try get the text rendering a bit nicer without the white silhouette.

  10. #8
    Join Date
    Dec 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Transparency with QWinWidget

    Hi, I'm having exactly the same problem, none of the following ways works for me, any suggestions? Thanks in advance.

    QWinWidget* pParentWidget = new QWinWidget(hParent);

    1. pParentWidget->setAttribute(Qt::WA_TranslucentBackground, true);
    pParentWidget->setWindowFlags(Qt::FramelessWindowHint);

    2. pParentWidget->setWindowOpacity(0);

    3. QPalette p(pParentWidget->palette());
    p.setColor(QPalette::Background, Qt::transparent);
    pParentWidget->setPalette(p);

    4. setMask isn't suitable for me, as the UI elements are dynamic.(e.g. tree view)

Similar Threads

  1. Tree selection - icon transparency (lack of)
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 8th January 2008, 21:48
  2. Transparency ... Again
    By EricF in forum Qt Programming
    Replies: 4
    Last Post: 1st December 2007, 19:52
  3. Speed, transparency, and drop issues with QGraphicsView
    By jefferai in forum Qt Programming
    Replies: 16
    Last Post: 30th June 2007, 16:14
  4. [QT3+XP] transparency and mouse tracking
    By incapacitant in forum Newbie
    Replies: 9
    Last Post: 17th February 2006, 18:49
  5. transparency
    By roms18 in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2006, 19:38

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
  •  
Qt is a trademark of The Qt Company.