How to set up QWidget only background transparency



  • I want to set up QWidget only background transparency, and I will draw some text and some others by qpainter on the widget. Using the setWindowOpacity(0.2) will make all things, including the text, etc, transparent. How should I modify it?

    #include "widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        setWindowOpacity(0.2);                       
        resize(200, 200);
        setWindowFlag(Qt::FramelessWindowHint);     
        QBitmap bmp(this->size());
        bmp.fill();
        QPainter p(&bmp);
        p.setRenderHint(QPainter::Antialiasing);
        p.setPen(Qt::NoPen);
        p.setBrush(Qt::black);
        p.drawRoundedRect(bmp.rect(),20,20);
        setMask(bmp);
    }
    
    Widget::~Widget()
    {
    
    }
    
    void Widget::paintEvent(QPaintEvent *)
    {
        QPainter painter(this);
        painter.setPen(Qt::blue);
        painter.setFont(QFont("Arial", 30));
        painter.drawText(rect(), Qt::AlignCenter, "Qt");
    
        update();
    }
    
    

    Qt 5.9.1 on windows 10
    Thanks in advance.


  • Moderators

    @Qt_crazyer
    try to set one (or all) of the following attributes (in conjunction to the already set Qt::FramelessWindowHint window flag):

    setAttribute(Qt::WA_NoSystemBackground);
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_PaintOnScreen);
    

Log in to reply
 

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