Turning off background fill on QDialog



  • Hi everyone, I have a problem I can't seem to figure out. I have a QDialog which I paint a image on that has transparency in it. When I paint it to the newly created QDialog box I get the image surrounded by a big white square. Apparently QDialog is filling the box with white then my image is painted over it allowing the white to show through the transparency. I have tried setting the setAutoFillBackground() to false no change. If I turn on the WA_TranslucentBackground then the entire image is not drawn. Is there a way to stop the QDialog box from filling itself with a color when created?
    Thanks -- Chris


  • Qt Champions 2016

    @Chrisw01

    WA_PaintOnScreen?



  • All that did was not display anything and throw errors in the application output box. Here's what I have so far.

    myQDialogBox::DialogBox(QWidget *parent, QString Name) : QDialog(parent) 
    {
          setMouseTracking(false);
          setAutoFillBackground(false); 
          setWindowModality(Qt::ApplicationModal);
          activateWindow();
    
          resize(image->width(), image->Height();
          QPainter p;
          p.setBrush(backgroundRole(), QBrush(*image));
          setPalette(p);
          show();  
    }
    

    It all works fine except it draws a white background before it draws my image and shows through the transparent parts of the image.

    Thanks -- Chris


  • Qt Champions 2016

    @Chrisw01
    Hello,

    resize(image->width(), image->Height();
    QPainter p;
    p.setBrush(backgroundRole(), QBrush(*image));
    setPalette(p);
    show();  
    

    You can't do this stuff - neither the painting, nor calling show() - in the constructor. You paint when there's a paint event (i.e. in a QWidget::paintEvent() override), and show() is called from the user of the widget, when the dialog should be shown. But that is after it's fully created.



  • I somehow don't see moving the code out of the constructor is going to fix the problem. I will try, but i'll lay odds the dialogs still going to be filled with a color which is what I need to avoid since I'm overlaying a QImage with transparency on it.


  • Qt Champions 2016

    @Chrisw01
    Perhaps I'm in error, I seem to have been misled by the typo (QPainter, where it should've read QPalette). In any case, can you show the relevant part of the code where you actually paint the image?

    I'm sorry I seem to have totally misinterpreted what the problem is. I'm not sure that having a texture pattern for a brush supports an alpha channel though, what do you expect to show under the transparent part of the image?

    Kind regards.



  • @kshegunov

    My Bad, it was supposed to be QPalette p;

    The part that actually paints the image is

    p.setBrush(BackgroundRole(), QImge(*image));

    Since the dialog is creating a window, it uses the system colors and creates a window filled with the system color (in my case white) then I overlay the QImage on top of it. (BTW I'm creating a Skinable GUI). If I use the WA_TranslucentBackground Attribute, then there is no white filled screen. However because I set background to Translucent then my setBrush fails because I'm painting to the background.

    I would have thought setAutoFillBackground being set to false would have stopped the QDialog from filling its background on creation.

    Here's the code.

    myDialogBox::DialogBox(QWidget *parent, QString Name) : QDialog(parent)
    {
        setMouseTracking(true);
        setAutoFillBackground(false);
        setWindowModality9Qt::ApplicationModal);
        activateWindow();
        setWindowFlags(Qt::Dialog | Qt::FramlessWindowHint);
    
        QPalette p;
        p.setBrush(BackgroundRole(), QBrush(*Image));
        setPalette(p);
    
    }
    

  • Qt Champions 2016

    @Chrisw01 said:

    Since the dialog is creating a window

    Do you mean that you're not passing a parent, or what window if that's not what you mean?

    I would have thought setAutoFillBackground being set to false would have stopped the QDialog from filling its background on creation.

    But even if that were the case, then setting a brush for the background would do nothing, wouldn't it?



  • Yes, I am passing it a parent, however the QDialog creates a new window on top of your current window. I pretty much have given up on this now, I even went as far as overloading the paintEvent for the dialog class and painting from within with the same outcome. And perhaps I misunderstood the setAutoFillBackground, I figured its purpose was to fill the background of the window. So for now I'll just remove the transparent section and it works fine.

    Thanks!!


  • Qt Champions 2016

    @Chrisw01

    Yes, I am passing it a parent, however the QDialog creates a new window on top of your current window.

    It doesn't create a new window when you pass a parent; Qt's widgets are alien by default. That's why I was asking. If the dialog is a child widget and it doesn't paint anything it will let you see the parent's contents.


Log in to reply
 

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