QDialog with transparent background color

I want to make the background QDialogtransparent so that I can see through the window. I ask because I want to use a translucent background image that creates the illusion of a "rounded corner window." Using is setOpacitynot an option for me, because I want all widgets to remain fully opaque.

Is there a way to achieve this without resorting to using native OS APIs?

+3
source share
1 answer

Use QWidget::setAttribute(Qt::WA_TranslucentBackground);. Please note that this also requires installation Qt::FramelessWindowHint.

This example works for me:

#include <QtGui>

class Dialog : public QDialog
{
public:
    Dialog() : QDialog(0, Qt::FramelessWindowHint) // hint is required on Windows
    {
        QPushButton *button = new QPushButton("Some Button", this);    
        setAttribute(Qt::WA_TranslucentBackground);
    }

};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog d;
    d.show();
    return a.exec();
}

Regarding rounded corners QWidget::setMask()will help you.

EDIT: , , QWidget::paintEvent():

#include <QtGui>

class Dialog : public QDialog
{
public:
    Dialog() : QDialog(0, Qt::FramelessWindowHint) // hint is required on Windows
    {
        setFixedSize(500, 500); // size of the background image
        QPushButton *button = new QPushButton("Some Button", this);
        setAttribute(Qt::WA_TranslucentBackground);
    }

protected:
    void paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        painter.drawImage(QRectF(0, 0, 500, 500), QImage(":/resources/image.png"));
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog d;
    d.show();
    return a.exec();
}
+10

All Articles