QPalette with multiple color background - c++

I know that we can use QPalette to set the background for a QLabel. But could I draw a multiple color background with QPalette? For instance, the half above in black and the half below in blue.
I couldn't find a setRect() function QPalette. Or should I use other class? Or do I have to draw the background with a painter?

The documentation for QGradient class states:-
The QGradient class is used in combination with QBrush to specify gradient fills
So, you can start by creating a gradient and setting that to a QBrush
QLinearGradient linearGrad(QPointF(100, 100), QPointF(100, 200));
linearGrad.setColorAt(0, Qt::black);
linearGrad.setColorAt(0.5, Qt::blue);
You can experiment with setting different colours at different stops, which range from 0.0, to 1.0
Use the gradient to create a brush...
QBrush brush(linearGrad);
The documentation for QPalette states: -
Colors and brushes can be set for particular roles in any of a palette's color groups with setColor() and setBrush().
So, using the setBrush function of QPalette, set the brush that is created with the gradient: -
QPalette palette;
palette->setBrush(QPalette::Window, brush);

Related

Get QPushButton Pressed Background Color

How can I get the pressed background color of a QPushButton?
if (isDown())
_BGColor = <pressed background color>; // how to get this???
else
_BGColor = palette().color(QPalette::Background); // this is to get the idle backcolor
Thanks in advance!!!
It's very difficult (if not impossible) to find a way to get the backgound color of a button when it's pressed, because it depends on the style, and it's not guaranteed that the style respects the palette.
However I suggest two different approaches:
You can set you own background color using style sheets (simpler) or implement the painting of the button yourself, using styles or reimplementing paintEvent(). See Customizing QPushButton
To paint over the button with the inverse color, you can set a composition mode to the painter in order to get the inverse color.
For example:
painter.setPen(QColor(255, 255, 255));
painter.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination);
(note that using this example, the inverse color of middle grey (128, 128, 128) is exactly the same color)
See QPainter::CompositionMode

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 C++ Can QGraphicsItem's paint() method access underlying pixels?

For visual markers in an imaging application on top of images I would like to enhance the contrast of said markers by using a fill color with high contrast to the local background (e.g. inverted). This requires for the object to read its background in a QGraphicsScene.
Is there an efficient (built-in) way of doing this or does it require something like rendering the scene without the marker, reading pixels in its position and then paint() the marker accordingly?
There is no straight way to get the rendered color of some point in QGraphicsScene. You should actually render the scene and see the color. One workaround is to render the scene to a QImage and pick the color from desired pixel:
QImage image=QImage(width,height,QImage::Format_RGB32);
QPainter painter( &image );
painter.setRenderHint(QPainter::Antialiasing);
myScene->render( &painter, image.rect(),QRectF(0,0,width,height), Qt::KeepAspectRatio );
painter.end();
QColor color = QColor(image.pixel(x,y));

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.