PDA

View Full Version : Transparent Widgets



CodeFreak
3rd August 2018, 18:06
Hi everyone,
I want to make a transparent window like:
12921

I tried following codes:


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

setAutoFillBackground( false );
setWindowFlags(Qt::FramelessWindowHint);
// setAttribute(Qt::WA_TranslucentBackground);
// setAttribute(Qt::WA_TransparentForMouseEvents);

QLinearGradient alphaGradient(rect().topLeft(), rect().bottomLeft());
alphaGradient.setColorAt(0.0, Qt::transparent);
alphaGradient.setColorAt(0.5, Qt::black);
alphaGradient.setColorAt(1.0, Qt::transparent);
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
effect->setOpacityMask(alphaGradient);
effect->setOpacity(0.8);
setGraphicsEffect(effect);
}
If I use "WA_TranslucentBackground" background will be omitted from the widget (you can select anything behind it !) but, I need a semi transparent window which its opacity/transparency can be adjusted.
Thanks.

d_stranz
3rd August 2018, 19:12
If I use "WA_TransparentForMouseEvents" background will be omitted from the widget

I don't think that is right. "Transparent for mouse events" doesn't mean the widget is visibly transparent, it just means it ignores mouse events and passes them to whatever widget(s) are underneath it.


I want to make a transparent window ... I need an opaque widget.

These two statements are contradictory. Which is it that you need - a widget that shows what is under it (transparent / translucent) or one that doesn't (opaque)?

CodeFreak
3rd August 2018, 19:19
Thanks for your hint.
I've edited the post.
I need a semi transparent window which its opacity/transparency can be adjusted.

d_stranz
3rd August 2018, 19:43
So you want the entire MainWindow to be translucent, toolbars, menubar and all? If not, then you should apply the effect only to the central widget.

I think you need to remove the setAutoFillBackground() statement, because you do want the widget to fill its background, and I think you should move the gradient code to the resizeEvent(), because otherwise the gradient will no longer match the widget's size when the window is resized. I don't know how the WA_TranslucentBackground attribute works in combination with a graphics effect, so you'll have to try it both ways.

An alternative is to set a gradient QBrush as the QPalette brush for the QPalette::Window color role.

CodeFreak
4th August 2018, 10:40
I want to make something like a semi transparent QTabWidget object which whole the background be semi transparent (even the icons) but the text of the icons and also whole the contents don't be.
To simplify the procedures, I'm trying to make a transparent window, first.
I tried following codes:


void MainWindow::paintEvent(QPaintEvent* ) {
QPalette pal = palette();
pal.setBrush(QPalette::Window, QColor(0, 0, 255, 0) );
setPalette(pal);
setAutoFillBackground(true);
}12927


void MainWindow::paintEvent(QPaintEvent* ) {
QPalette pal = palette();
pal.setBrush(QPalette::Window, QColor(0, 0, 255, 255) );
setPalette(pal);
setAutoFillBackground(true);
} 12926


void MainWindow::paintEvent(QPaintEvent* /*event*/) {
// QColor backgroundColor = palette().light().color();
// backgroundColor.setAlpha(0);
QColor backgroundColor =Qt::TransparentMode;
QPainter customPainter(this);
customPainter.fillRect(rect(),backgroundColor);
} 12925


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint);
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
effect->setOpacity(1.0);
setGraphicsEffect(effect);
} 12928


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint);
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
effect->setOpacity(0.0);
setGraphicsEffect(effect);
} 12929

Anyway "QGraphicsOpacityEffect" is not the thing that I want.
I also tried many other things...

d_stranz
5th August 2018, 18:56
Saw this in an answer on another forum (https://forum.qt.io/topic/2599/solved-semi-transparent-widget/7):



ChildWidget( QWidget* parent = 0 ) : QLabel( parent )
{
QPalette pal = palette();
pal.setBrush(QPalette::Window, QColor(0, 0, 255, 128) );
setPalette(pal);
setAutoFillBackground(true);
}


Essentially what I suggested in an earlier post.

CodeFreak
5th August 2018, 21:38
Main Window: With a method like this a black background still exist (in main window) even you set the alpha to 0.
Child Widget : A simple empty "child QWidget" will be transparent or semi transparent but if you want to make another Widgets (like QTabWidgets) transparent, it do not work.

d_stranz
6th August 2018, 20:32
OK. I really don't understand what it is you are trying to do, so good luck.

CodeFreak
7th August 2018, 16:50
I wan to make an application which be totally semi-transparent; Main Window and all the widgets (TabWiget, PushButton, Label, ProgressBar and etc.) inside it be semi-transparent.

Opacity function influences whole the MainWindow and its children; title, close button, minimize and maximize button and content will be affected by opacity function. Then its not the thing that I want.

Qt::WA_TranslucentBackground ; for Main Window any content behind it, is accessible for all mouse/touch events (In Windows 8.1).
> And I can't use it for TabWidget, PushButton, Label, ProgressBar and etc.

It is almost like what I want:
12930

Anyway, thanks for your reply.