I want to brush my sub widget with QLinerGradient. I have created ui by using QtDesigner.

But I can not brush this widget by using this code.(ui.colorBarWidget is normal QWidget was created by QtDesigner.)
QPalette palette;
QLinearGradient gradient(ui.colorBarWidget->rect().topLeft(),ui.colorBarWidget->rect().topRight());
gradient.setColorAt(0, Qt::blue);
gradient.setColorAt(0.2, Qt::green);
gradient.setColorAt(0.4, Qt::red);
gradient.setColorAt(0.6, Qt::yellow);
gradient.setColorAt(1, Qt::cyan);
palette.setBrush(QPalette::Base, gradient);
ui.colorBarWidget->setPalette(palette);
In addition this code works in stand alone QWidget application.This is its output:

But I can not do same thing in my design. I can do this with styleSheet
ui.colorBarWidget->setStyleSheet( "background-color: qlineargradient( x1:0 y1:0, x2:0 y2:1, stop:0 blue, stop:1 red )" ); /* works */
but why I can not do this with QPalette.
Thanks in advance.
I found the solution. If you use after setting palette:
ui.colorBarWidget->setAutoFillBackground(true);
This property is default false. So you should enable it then everything is fine. But also you should consider the size, fixed size better for this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With