How to set up QWidget only background transparency
Unsolved
General and Desktop
-
wrote on 22 Mar 2018, 13:40 last edited by
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. -
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.@Qt_crazyer
try to set one (or all) of the following attributes (in conjunction to the already setQt::FramelessWindowHint
window flag):setAttribute(Qt::WA_NoSystemBackground); setAttribute(Qt::WA_TranslucentBackground); setAttribute(Qt::WA_PaintOnScreen);
1/2