Work around for changing tool button's background color
-
Lets explore the rectangle option, how do I fix it to a particular button?
-
Lets explore the rectangle option, how do I fix it to a particular button?
@Omni_Philm
hi
its not fixed to it. its painted on top. -
@mrjj said in Work around for changing tool button's background color:
Q_OBJECT
public:
explicit ColorButton(QWidget *parent = nullptr) : QToolButton(parent) {}
protected:
virtual void paintEvent(QPaintEvent *) override
{
QStylePainter p(this);
QStyleOptionToolButton opt;
initStyleOption(&opt);
p.drawComplexControl(QStyle::CC_ToolButton, opt);
p.setBrush(QColor(255,0,0,100));
p.setPen(Qt::NoPen);
p.drawRect(4,4,width()-8,height()-8);
}So I finally had a chance to try out your code. I think that it holds promise but the text will need to be black. Is there any way that I can force draw the text on foreground for everything?
Also, the pic you have in shows that the text is not really colored with red. However, for me, the text is colored with red but the black is seeping through?
-
Any thoughts?
-
Any thoughts?
@Omni_Philm
Hi
The overlay is transparent so it only color the black text a bit.
The (QColor(255,0,0,100)); , the last 100 is how trans it is.Well you could remove the text from opt. (set to QString() )
so there is no text for
p.drawComplexControl(QStyle::CC_ToolButton, opt);
to draw
and then use p.drawText
after
p.drawRect
to have text on top.
However, if u use any text-align etc on the button, those will break unless you handle that too for the
custom drawing. -
Hello, I am trying to use the drawText function. Here is what I have so far:
p.setPen(QColor(0,0,0)); p.setFont(this->font()); p.drawText(this->frameGeometry(), Qt::AlignCenter, tempText);
What happens is that no text is actually drawn. I figured I would use a rectangle on the QToolButton to bind the text to. That way, I can preserve my alignment.
-
Hello, I am trying to use the drawText function. Here is what I have so far:
p.setPen(QColor(0,0,0)); p.setFont(this->font()); p.drawText(this->frameGeometry(), Qt::AlignCenter, tempText);
What happens is that no text is actually drawn. I figured I would use a rectangle on the QToolButton to bind the text to. That way, I can preserve my alignment.
@Omni_Philm
what do u set tempText too ? -
@Omni_Philm
what do u set tempText too ?Oops, forgot to include that line in the example:
but
tempText = opt.text;
Edit:
here is the full drawing code:
QString tempText; QStylePainter p(this); QStyleOptionToolButton opt; initStyleOption(&opt); tempText = opt.text; opt.text = QString(); p.save(); p.drawComplexControl(QStyle::CC_ToolButton, opt); p.setBrush(QColor(255,0,0,100)); p.setPen(Qt::NoPen); p.drawRect(4,4,width()-8,height()-8); // p.setBrush(QColor(0,0,0)); p.setPen(QColor(0,0,0)); p.setFont(this->font()); p.drawText(this->frameGeometry(), Qt::AlignCenter, tempText); p.restore();
-
Any thoughts?
-
Ok, I got, had to change one of the lines of code to:
p.drawText(this->contentsRect(), Qt::AlignCenter, tempText);