QT Transparent Layout - qt

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.

Related

Qt: Setting gradient on Layout

I have a QHBoxLayout with several items in it. In the space occupied by the QHBoxLayout, I want a vertical gradient from black to white (behind the items in the QHBoxLayout). However:
QHBoxLayouts do not have style sheets, so that's out
QHBoxLayouts do not have palettes, so that's out
And these are the only two methods of setting gradients that I've found (they were all intended to work on widgets). I tried setting the stylesheet of the parent widget to include:
QHBoxLayout {
background-color: red;
}
to see if css could be worked in that way, but this does nothing.
Is it possible to give a layout a background gradient with Qt?
(I am using PyQt5, if that's relevant)
I'm not expert to PyQt, but in plain Qt you can put your QHBoxLayout inside a QWidget and then apply a layout to the widget (the buttons above the designer).
This is what the object tree looks like when done (the selected item has the gradient)
And this is how my little mock up came out. (If you like the buttons to be non-transparent, add a stylesheet for the background)

QT Tab bar's top highlight with stylesheet

I have a problem when trying to change the color of QTabBar's top line (blue line in the picture below).
Is this a separate part of tabBar (like scroller or tear) or its top border ? And how can I change its color with styleSheet and leave the other parts of tabBar unchanged?
P.S. : My tabBar::styleSheet returns an empty string, so I can't get current style and make changes in it.
If you're using a "system" style, you may not be able to change the color of the line (cause representation of UI elements is not handled by Qt but by the system).
You should define a complete style for QTabBar (and maybe QTabWidget too) that you can customize as you wish.
See the Qt Style Sheets Examples page.
Problem solved:
setStyleSheet("QTabBar::tab:selected { selection-background-color: red; }");

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.

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.

How to make the QLabel background semi-transparent?

I have created QLabel *msgLbl. How do I make the msgLbl background semi-transparent?
The easiest way is probably to call setStylesheet(). If you're using Qt Designer you can also set the stylesheet from within the designer window (look in the properties bar on the right.
You want to set the background-color attribute, so something like
msgLbl->setStyleSheet("background-color: rgba(255, 255, 255, 10);");
would be the simplest way to do what you describe.
Having said that, you might also want to think about stylesheet inheritance. For example, you might want to set the background-color for a number of QLabels, all children of a parent widget. You can do that using css-style selectors in a stylesheet set on the parent widget (read this for more information).

Resources