Clipping of (OR maybe background colour of) QToolbar Icons
-
I have a toolbar that does this in its ctor:
constexpr int radius = 27; : QColor colour = palette().window().color(); colour = colour.lighter(300); colour.setAlpha(205); QString styleSheet(QString( "QToolBar " "{background-color: %1; " "border-radius: %2px;}; " ) .arg(colour.name(QColor::HexArgb)) .arg(radius) ); setStyleSheet(styleSheet);
I thought that this would clip the icons to the new outline but what I get is:
So the icons aren't being clipped to the new shape.
Should I expect that to work?
The png files that define the icons have their background set to transparent (outside the circle in the centre).
-
@Perdrix
I know nothing :) But (provided I understand right) I would expect:QToolButton{border: none; background-color: transparent;}
(I assume you have the correct transparency on your icons. Perhaps you should confirm this?)
Just OOI isn't that what https://stackoverflow.com/questions/69755056/how-to-set-qaction-to-have-transparent-background-inside-qtoolbar says and is similar enough to yours??
-
Update: They aren't buttons, they are icons.
I'm guessing that the paint method is filling the background with "window" colour before it paints the icon image (set with setFile()).
Is there an easy way to get them to prefill with transparent colour so the toolbar shows through except where the icon itself is non-transparent?
It's only the currently active icon that does this ...
Thanks
David -
@Perdrix
Hi
As far as I know, QToolButtons are auto-created when you insert icons.Settings a round border will not clip anything as the clipping is the Widgets area and
even if the frame/border is drawn round, the actual area is still square.Try setting QToolbuttons background
QToolButton { background-color:transparent; }
-
@mrjj I changed the ctor so it starts with:
constexpr int radius = 27; QColor colour = palette().window().color(); colour = colour.lighter(300); colour.setAlpha(205); QString styleSheet(QString( "QToolButton {background - color:transparent;} " "QToolBar " "{background-color: %1; " "border-radius: %2px;}" ) .arg(colour.name(QColor::HexArgb)) .arg(radius) ); setStyleSheet(styleSheet);
Unfortunately, it didn't seem to help any:
-
-
@Perdrix
I know nothing :) But (provided I understand right) I would expect:QToolButton{border: none; background-color: transparent;}
(I assume you have the correct transparency on your icons. Perhaps you should confirm this?)
Just OOI isn't that what https://stackoverflow.com/questions/69755056/how-to-set-qaction-to-have-transparent-background-inside-qtoolbar says and is similar enough to yours??
-
If anything else fails, then you can try setMask
If does make the area round/as the icon but can a bit annoying to align.
More over, the checked default drawing might not be visible.#include "QBitmap" // must have to use setmask .... setStyleSheet(styleSheet); auto list = ui->toolBar->findChildren<QToolButton *>(); for (auto *w : list) { w->setAutoRaise(false); QIcon ico = w->icon(); QPixmap pix = ico.pixmap(ui->toolBar->iconSize()); w->setMask(pix.mask()); }
-