How to make the QLabel background semi-transparent? - qt

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).

Related

Qt dynamically change bg color without overwriting other styles

Okay what I am aiming for is a way to set the background color of a QGroupBox without overwriting any other styles that might be set on that object before.
Intuitively I'd use a QPalette for doing this but since my application is using stylesheets, I can't use palettes (using them just has no effect whatsoever).
Thus I am left with setting the background color via stylesheets. If the only style I want to set this was the background color, that'd be easy:
myBox->setStyleSheet("background-color: red;");
This however will overwrite all stylesheets that have been set on myBox before this call. In my case I am setting the font-size at a different place but on the same object and also via stylesheets.
So the next idea is to append to the existing stylesheet like this:
myBox->setStyleSheet(myBox->styleSheet() + " background-color: red;");
That's working so far, but the problem is that I want to change the background color rather frequently (not only once or twice) and while using the above method for the successive color changes (provided I append to the the stylesheet instead of prepending to it), it makes the stylesheet continually grow larger and larger. Furthermore I'd eventually be setting the background color hundreds of times in one and the same stylesheet just so I can have the currently active color as the final value. Parsing this seems like a huge waste of computational resources.
I am therefore looking for a way to change the background color of my box only. The method must preserve all attributes that are currently set in a stylesheet on my object (except the background color of course) and should not make the stylesheet grow to infinity without actually adding new information to it. And ideally the solution is not cascading the background color to children of my box.
How is something like this usually handled within Qt?
A possible solution that I could come up with would be to create a wrapper around the QGroupBox and introduce the style attributes I want to set on it as member variables and then creating the stylesheet based on the values of the member variables each time any of them changes.
This seems like a solution that doesn't scale very well and to me it seems like there should be a standard solution to this that doesn't require manually creating such wrappers every time...
You can have a selector like on CSS, example below:
QGroupBox { background-color: #fbca10;}
Or if you want more specific you can have the accessibleName as the selector also, example:
QGroupBox[accessibleName="mybox"] { background-color: #fbca10;}
the accessibleName is property on QWidget. You can set it on qt designer or by code.

Setting single stylesheet property in Qt

I'm trying to create vertical lines in a QTreeView; rather than painting over lines as suggested by some answers I am using a stylesheet to modify the right border of the cells in the treeview like so:
ui->tripsTreeView->setStyleSheet ("QTreeView::item:!last { border-right: 0.5px solid lightgray ;}");
Unfortunately this resets all other stylesheet properties on the treeview, making it unusable (white on white text, the expand triangle gets a blue background, custom background colours disappear, etc).
Questions:
Has someone managed to set a stylesheet for a single property in Qt?
Or found a way to extract the current applied stylesheet? I would then do a search-and-replace to add/set the proper border. Unfortunately ui->tripsTreeView->styleSheet() returns only an empty string (meaning it uses the deault stylesheet, but what the heck is the default stylesheet?)
When no stylesheet is set Qt uses a style which matches the style of the OS. When you try to change the stylesheet of even a single property the whole stylesheet of the control is replaced with a default one. I am not sure if the default stylesheet can be extracted. Probably a custom delegate with paint() function reimplemented can do the trick.

Modify button form in Qt

I'm new on Qt and I'm looking for some way to modify the form of a QPushButton, but I didn't find something on the web.
Is it possible and if so, how can I do this?
Thx
If you want to change how the QPushButton looks, you can use the Qt style sheets. If you want to put an image on the button, you can take advantage of the "background-image" property of the QPushButton
QPushButton
{
background-image: url("alpha.jpg");
}
You can change other properties like "border", "background-color", etc. Your question needs to be more specific as to what visual elements you need. Go through the documentation, it will help you.
One way to go about getting a different for the QPushButton is to make the necessary image available. Get the necessary image and make sure it has a transparent background. Use could use other attributes of QPushButton like, "background-color: transparent" and without any border - "border: none".
Hope it helps!

Accessing padding from stylesheet in QT

I have a custom QWidget subclass that lays out a number of children. In my stylesheet I define a background, which works fine. I also define padding, which doesn't work. I clearly need to provide support for this myself.
To do that, I need to be able to find out what padding is set in the stylesheet for my widget. I do not wish to parse the stylesheet myself, that would not make much sense. How can I access the top, left, bottom and right padding set in the stylesheet?
Thanks in advance,
Your custom widget has to inherit from a widget that supports the "box model" (you can find which widgets do on that page), and then you can use QWidget::contentsRect() to get the... content rectangle :

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