How to get widget background QColor - qt

I am trying to find out the background color of a QWidget or QGLWidget so that I can use it with qglClearColor() to make the OpenGL part appear natively within the widget (without for example a black background).
I figured I could fetch backgroundRole() of my widget but I am having problems converting it to a QColor. There is QPalette::color(QColorRole) but it isn't static and I have no idea how I would have to create an instance of QPalette to do the transformation.

Use QWidget::palette().color(QWidget::backgroundRole())to receive the color of the background color role for that widget. Obviously, this should also work with any class that inherits QWidget.

Related

How can I access Qt's used border color for use in qtstylesheets widget styling (via QPalette)?

I can use different colors used in a Qt application via QPalette to style widgets via qtstylesheets, e. g. something like background-color: palette(base). However, I didn't find the color used for drawing the border of some widgets like QGroupBox or QFrame.
It it possible to access this color (via QPalette?) as well for use in qtstylesheets widget styling?

Setting background color of system background for widgets, on a QGraphicsScene

In my QGraphicsScene, I would like to set the background brush to the default widget background - but I can't get it.
Kinda like, for my QGraphicsView,
setBackgroundRole(QPalette::Window);
or
setBackgroundBrush(palette().background().color());
(but setting this I see nothing happening)... I also see nothing happening if I set the view color to a bright red).
So I thought I must set color directly on the QGraphicsScene.
For the QGraphicsScene I am trying all sort of combinations like
setBackgroundBrush(QPalette::color(QPalette::Background));
Nothing will even build, seems I require an object (? a widget ?) - but my scene may not have a widget parent... and all I want is a default palette, I thought there would be a generic way to get that color without having a widget ?
On the scene, this will work...
setBackgroundBrush(Qt::red);
No clue why the view won't show color (even if I set on the view, red brush and on the scene transparent).
You may retrieve the QApplication's current style using the static method style(). From there, you may access the QStyle's standard palette using standardPalette(). Use QPalette's brush method to get a brush for a given ColorRole. Putting it all together you get...
QApplication::style()->standardPalette().brush(QPalette::Background)
This may not be the color you are expecting. Check out the documentation on http://doc.qt.io/qt-4.8/qpalette.html, and try different ColorRole values until you find what you're looking for.
Create a temporary widget instance just to access its palette and get the background color:
QColor bgColor = QWidget().palette().background().color();
But I think you should set the background color in the QGraphicsView widget. You can do that by changing its stylesheet. Something like:
QColor bg = ui->graphicsView->palette().background().color();
ui->graphicsView->setStyleSheet(QString("background-color:") + bg.name(QColor::HexArgb));
Setting a transparent background also works.

QWidget transparent background (but not the children)

I have a QWidget that contains a QPixmap and a QComboxBox in its Layout. I would like to set the background of the widget transparent (but I want to show the QPixmap and the QComboBox normally). How do I do that?
You can use the attribute
widget->setAttribute(Qt::WA_NoSystemBackground);
Qt documentation :
Indicates that the widget has no background, i.e. when the widget
receives paint events, the background is not automatically repainted.
Note: Unlike WA_OpaquePaintEvent, newly exposed areas are never filled
with the background (e.g., after showing a window for the first time
the user can see "through" it until the application processes the
paint events). This flag is set or cleared by the widget's author.
It is all well-explained in QWidget documentation:
http://doc.qt.io/qt-5/qwidget.html#transparency-and-double-buffering

Semi-transparent QWidget over QGLWidget: Strange results

I have a full size QGLWidget which paints the application background using QPainter (might change to native openGL commands in the future).
On top of this QGLWidget I use QWidgets (non-GL) for the user interface elements. These are, for example, QLineEdits and QPushButtons. I put them into a custom painted QWidget which uses semi-transparent background painting. The paintEvents of the QLineEdit and QPushButton are overwritten and use semi-transparent backgrounds, too.
The whole UI should look like the following (This is a screenshot where I disabled OpenGL and used QWidget instead of QGLWidget for the background. Note the semi-transparent top bar which also draws a shadow (within its own region)):
When the QLineEdit has the focus, it should have a higher opacity but still not fully opaque:
So now, with OpenGL enabled (The background then is a QGLWidget), the semi-transparent widgets above don't paint on top of the background but on (it seems to be) uninitialized data. The image shining through the top bar is sometimes the whole window itself and sometimes other windows currently being on my desktop.
This currently looks like the following (In this screenshot, the data on which the semi-transparent painting operations are painted on seems to be an image of the widget itself, having an offset.):
When I wrote text into the line edit (here: "This is some text which has been there before!"), removed it and set the focus back to the background widget (so the magnifier icon and the placeholder text appear), the previously painted things still shine through (Note that the visible border should not be visible anymore, but also still shines through):
So the problem is: Instead of being painted on top of the underlying widgets, the semi-transparent widget is painted on top of the old results, initially being something like "uninitialized memory".
Why does this happen? How can I solve the problem?
Things you should know before answering:
The background scene is a composition of tiles which are rendered off-screen. So it can be painted very fast and repainting of the background for every little change of the overlay isn't problematic.
The top bar is a custom QWidget with manual painting and arrangement of the contained two widgets (the button and line edit).
The two widgets overwrite the paintEvent, only draw their own (semi-transparent) background when they have focus and don't use frames already provided by Qt. So the white border in the second screenshot is drawn in my custom paintEvent.
I want the background and the composition of overlay widgets to be separately implementable. The background is an AbstractMapView which has some concrete map view classes. The whole window is an AbstractView (there are multiple concrete views, too), which contains both a concrete map view and the overlay widgets, composed in a way itself decides. Therefore, I don't want the logic of the overlay widgets to be part of the underlying map view. (I hope you understood this, as it is a bit complicated.)
This sounds like an issue where the GL content (i.e. your background aka the QGLWidget) is not in the Qt context. While I'm not a pro on GL painting with Qt, you may want to look at this discussion regarding GL painting and a QLabel for some direction/potential hints.
http://www.qtcentre.org/threads/40335-QLabel-on-top-of-a-QGLWidget-background-issue
In short, we here at the office use OpenGL painting and offscreen rendering of maps and it's very important to make sure Qt is aware of the pixels so your foreground widgets can have the semi-transparency applied to their backgrounds.
The particular product we use also renders the map in tiles and supports providing the GL output in a buffer (i.e. it's call a snapshot and is provided as a bitmap) at which point we use the paintEvent of a regular QWidget to paint the buffer so that the painted pixels are in Qt context.
You can define a Qframe with Qt::SplashScreen flag as the search box and set its opacity. Put your widgets inside it such as the search textbox and positon it where it should appear on the mainwindow. It will also be a good idea to reposition it as the mainwindow is moved or resized overriding its moveEvent.

QT Transparent Layout

I am using a vertical layout (QVBoxLayout) to manage buttons. I would like to make its background color as 50% black transparent. Is it possible ?
sw
Depending where you want the border of the transparent area, you will need to group the buttons in a widget (as SigTerm said) and then you can assign a color either via the palette
QPalette palette = widget->palette();
palette.setColor(QPalette::Window, QColor(100,100,100,100));
widget->setPalette(palette);
or use a stylesheet
widget->setStylesheet("QWidget{background-color: rgba(100,100,100,100);}";
the stylesheet has the advantage that you can style all of your application from one spot that is not in the code and set an application wide stylesheet via QApplication::setStylesheet(QString)
Ahem... it's been a while since I used Qt, but as far as I know, QVBoxLayout has no background color, so no, it isn't possible. Layout isn't a widget and it isn't being painted at all, it only manages child widget sizes.
If you want to create colored layout, you'll probably have to create a widget with whatever color your want, and then parent QVBoxLayout to that widget.
It'll become more fun if you want color of all layout's children to be affected by color of QVBoxLayout's parent, but I think that "Embedded Dialogs" demo from Qt4 demo may give you an idea about how it can be done.

Resources