Cannot make transparent (translucent) background using Qt styles



  • I have used the Qt widgets->styles example to test this functionality.

    The only change i have made is that in the method

    void NorwegianWoodStyle::polish(QWidget *widget)
    

    I have added two lines of code:-

    if(widget->isWindow())
    { 
    widget->setWindowFlags( Qt::FramelessWindowHint ); 
    widget->setAttribute( Qt::WA_TranslucentBackground ); 
    }
    

    However the output is not what i am expecting. Its showing output with black background in QT 5.7. However when building the same example with Qt 4.8 I am able to see the transparent background. The following screenshots in the attachment section will help you more in understanding the problem.

    Qt 4.8
    Qt 5.7

    Could you please tell me if this is Qt bug and is there any alternative resolution to this problem.


  • Moderators

    Hi! You need to put your extra code in the dialog's constructor:

    WidgetGallery::WidgetGallery(QWidget *parent)
        : QDialog(parent)
    {
        setWindowFlags( Qt::FramelessWindowHint );
        setAttribute( Qt::WA_TranslucentBackground );
        //...
    

    Wouldn't call it a bug, just different behaviour between major Qt versions.



  • Thanks for the reply. But what i am trying to do is to switch the style from opaque background to transparent and vice versa via user input. So when i launch my application it should be opaque and when user selects transparent mode i repolish again the application to show transparent. At first while launching the application the transparency is working fine but thats before calling show method. However if i do the same using styles its not working. I tried using pallete as well and set it to widget background role however i am still getting the black background. I tried debugging the Qt code as well and what i saw is that when we set WA_TransulentBackground to widget before show its executing the create_sys() method and in that we set the QSurfaceFormat alphabuffersize to 8 and its working fine. However if we set the same attribute after the show function its executing updateIsTranslucent function and there its unable to set the alphabuffersize and because of that i am getting black background. Correct me if i am wrong however i think this is more or less seems to me as a Qt bug. Note that i need opaque background first and transparent later based on user input.

    Thank you


  • Moderators

    i need opaque background first and transparent later

    Didn't know that's supported. So you might be right calling it a bug. Maybe you want to check the bug database or ask the developers on the mailing list.



  • @Wieland Thanks for the quick reply. Now let me tell you one more thing. I have an application which works like this with Qt 4.6.2 and i have tried the same in Qt 4.8.4 and it works perfectly fine and switching from opaque to transparent worked as expected. However when i ported my application to Qt 5. 6.2 as its an LTS release i found this strange behaviour (black background on switching to transparent) and made me wonder why such a weird output. What i mean to say here is that it used to give switching of background functionality. So i hope this should work in the latest version as well.


  • Moderators

    This is QTBUG-57531.



  • @Wieland Okay. Thank You


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.