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

Murphy, Sean smurphy at walbro.com
Thu Mar 31 16:20:20 CEST 2016


Try changing your setBrush line to:
  palette.setBrush(QPalette::Window, gradient);
When I do that I get the following:
[cid:image001.png at 01D18B36.349FAE70]

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);
}

[cid:image002.png at 01D18B36.B1E69AB0]

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.

[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:

[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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160331/494c17bf/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 21116 bytes
Desc: image001.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160331/494c17bf/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 20734 bytes
Desc: image002.png
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160331/494c17bf/attachment-0001.png>


More information about the Interest mailing list