[Interest] QPalette or QLinearGradient does not work on sub widget

Berkay Elbir berkayelbir at gmail.com
Wed Apr 6 13:54:49 CEST 2016


Hello,

Thank you for your answer. But I tried same code, still can not see color.
I also fixed the size

    ui->colorBarWidget->setFixedSize(71,275);


I put a button to test my widget is problem or not but it shows
button. I do not understand. Is Your designer file different or what
is the version of your qt?



​



My versions are 5.5.1 or 5.6.0


Thanks,


Berkay


On Thu, Mar 31, 2016 at 5:20 PM, Murphy, Sean <smurphy at walbro.com> wrote:

> Try changing your setBrush line to:
>
>   palette.setBrush(QPalette::Window, gradient);
>
> When I do that I get the following:
>
>
>
> Also, note that this line
>
>     QLinearGradient gradient(ui->colorBarWidget->rect().topLeft(),ui->
> colorBarWidget->rect().topRight());
>
> Depending on when you call it may not give you the results you’re
> expecting. If I put your code in my MainWindow constructor I get the
> picture above. This is because after construction, a resize event happens,
> so the original gradient calculation was using a different rect width than
> what the user sees. If you instead create a slot that is connected to a
> single shot timer like so, you get a gradient that better utilizes the
> widget’s size.
>
>
>
> #include "mainwindow.h"
>
> #include "ui_mainwindow.h"
>
>
>
> #include <QPalette>
>
> #include <QLinearGradient>
>
>
>
> MainWindow::MainWindow(QWidget *parent) :
>
>     QMainWindow(parent),
>
>     ui(new Ui::MainWindow)
>
> {
>
>     ui->setupUi(this);
>
>
>
>     initTimer = new QTimer(this);
>
>     connect(initTimer, SIGNAL(timeout()), SLOT(init()));
>
>     initTimer->setSingleShot(true);
>
>     initTimer->start(0);
>
> }
>
>
>
> MainWindow::~*MainWindow*()
>
> {
>
>     delete ui;
>
> }
>
>
>
> void MainWindow::init()
>
> {
>
>     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::Window, gradient);
>
>
>
>     ui->colorBarWidget->setPalette(palette);
>
> }
>
>
>
>
>
> If you want this gradient to be correct all the time, I think you’ll
> either need to set that widget to a fixed size, or you’ll have to create
> your own custom widget and overload resizeEvent(QResizeEvent*) to keep
> recalculating the gradient when the widget gets resized.
>
>
>
> Sean
>
>
>
> *From:* Interest [mailto:interest-bounces+smurphy=
> walbro.com at qt-project.org] *On Behalf Of * Berkay Elbir
> *Sent:* Thursday, March 31, 2016 8:19 AM
> *To:* interest at qt-project.org
> *Subject:* [Interest] QPalette or QLinearGradient does not work on sub
> widget
>
>
>
> Hello all,
>
> I want to ask a question about QPalette and QLinearGradient.
>
> I want to brush my sub widget with QLinerGradient. I have created ui by
> using QtDesigner.
>
> [image: enter image description here] <http://i.stack.imgur.com/5JzSK.png>
>
> 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:
>
> [image: enter image description here] <http://i.stack.imgur.com/z5fc5.png>
>
> 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.
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160406/7350c55b/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mainWindow.PNG
Type: image/png
Size: 5630 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160406/7350c55b/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 20734 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160406/7350c55b/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 21116 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160406/7350c55b/attachment-0002.png>


More information about the Interest mailing list