How to add picture containing transparency to the window background using code? - c++

How to set a png file containing transparency to the window background
using only code and not css/stylesheet?
With the following code the picture is added however, the background became black in the parts where
the image is not painted
ui.setupUi(this);
QPixmap bkgnd(":/pic");
//bkgnd = bkgnd.scaled(this->size(), Qt::IgnoreAspectRatio);
//bkgnd.fill(Qt::transparent);
QPalette palette;
//palette.setColor(QPalette::Window, Qt::transparent);
palette.setBrush(QPalette::Window, bkgnd);
this->setPalette(palette);
//this->setAttribute(Qt::WA_TranslucentBackground, true);
Commented are things i already tried.

Related

Render QWidget in QPixmap with system background

When I render a QWidget with a QOpenGLWidget child with transparency objects, I observe a solarization effect when I tried to take a screenshot like the following picture:
The problem comes with the system background because when I remove it, the problem disappear:
.
My question is: What is the solutions to render a QWidget on a QPixmap with the system background?
Here my render code:
QWidget widget;
QPixmap pixmap(widget.size());
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
widget.render(&painter, QPoint(), QRegion(), QWidget::DrawWindowBackground /* the problem is this render flag */ | QWidget::IgnoreMask | QWidget::DrawChildren);
pixmap.save("screenshot.png");
The property setAutoFillBackground(true); and the method grab(); do the same like the first screenshot.
EDIT: There is no OpenGL problem. When I mix two pixmaps (the first with the background and the second with widget children), the solarization problem is still present.
A solution is to set a QPalette with Window role to black in my QOpenGLWidget.
QPalette currentWidgetPalette{ palette() };
currentWidgetPalette.setColor(QPalette::Window, Qt::black);
setPalette(currentWidgetPalette);
EDIT: I found a solution to paint destination in tansparent and source with my widget to compose the pixmap correctly.
painter.setCompositionMode(QPainter::CompositionMode_Destination);
painter.fillRect(result.rect(), Qt::transparent);
painter.setCompositionMode(QPainter::CompositionMode_Source);
widget.render(&painter, QPoint(), QRegion(), QWidget::DrawWindowBackground | QWidget::IgnoreMask | QWidget::DrawChildren);

Change border color of QWidget using animation

I have tried to change border color of Qwidget for two days but Doesn't work.
so I refered to another code which is posted on the stackoverflow about moving widget. it worked perpectly but I can't change border color using QPropertyAnimation. give some tips for this problem.
QPropertyAnimation *animation = new QPropertyAnimation(ui.defectView, "border-color");
animation->setDuration(2000);
animation->setStartValue(QColor(0, 0, 0));
animation->setEndValue(QColor(240, 240, 240));
animation->start();
There is no QWidget property called "border-color". All it's properties are shown in QWidget documentation. But you can change this color using QSS and animate it. Also consider custom paintEvent depending on your needs.

How to set the background color of a OpenGL enhanced QGraphicsView?

I have a custom QGraphicsView class where I try to set the background color in its constructor.
These work in non-OpenGL mode:
QColor backgroundColor(50,50,50,255);
setBackGroundBrush(backgroundColor)
and
QPalette myPal = this->palette();
myPal.setColor(this->backgroundRole(), backgroundColor);
this->setPalette(myPal);
However in OpenGL mode the first produces a white screen clearing, while the second clears the widget with the given color. I can also see for a few milliseconds the actual item ought to be drawn in between repaints.
If I don't set the background color at all a white background is set, and everything functions just fine.

How to set QComboBox background color on drop down state?

I can change QComboBox color like this:
QPalette palette = ui->selectSource->palette();
palette.setColor(QPalette::Active, QPalette::Button, Qt::white);
palette.setColor(QPalette::Inactive, QPalette::Button, Qt::white);
ui->selectSource->setPalette(palette);
It becomes white, but when its in drop down state it still have some gray color (default).
How to change this?
You could apply one more palette to the combo box's drop down view too. To get the pointer to the drop down view you can use the QComboBox::view() function. So, your code will look like:
QPalette palette = ui->selectSource->palette();
palette.setColor(QPalette::Active, QPalette::Button, Qt::white);
palette.setColor(QPalette::Inactive, QPalette::Button, Qt::white);
QPalette view_palette = ui->selectSource->view()->palette();
view_palette.setColor(QPalette::Active, QPalette::Background, Qt::white);
ui->selectSource->view()->setPalette(view_palette);
You should also set QPalette::Base role. Quote from the Qt documentation about QPalette::Base :
Used mostly as the background color for text entry widgets, but can
also be used for other painting - such as the background of combobox
drop down lists and toolbar handles. It is usually white or another
light color.
So you should also have :
palette.setColor(QPalette::Base, Qt::white);

QT Background-Image

I tried to add a gradient background on my main QWidget by adding (background-image:url(images/background.png) to its stylesheet but I noticed HUGE performance drops and I haven't written any code yet.
background image is a gradient , 16bit 1x800 px png.
So my question is, how can I add a nice gradient to my QWidgets / QFrames without slowing down the program? Using only the designer.
Try this:
QPalette thePalette = this->palette();
QLinearGradient gradient(0, 0, 0, 50);
gradient.setColorAt(0, QColor(227,177,27));
gradient.setColorAt(0.25, QColor(170,177,167));
gradient.setColorAt(1, Qt::white);
QBrush brush(gradient);
thePalette.setBrush(QPalette::Window, brush);
setPalette(thePalette);
and modify freely the colors and the positions. Although that this is code, it might be useful.