PDA

View Full Version : QprogressBar Style Sheets: double linear gradient



Beppe
6th May 2009, 09:52
Hi all. I wuold like to obtain a ProgressBar like this.

I think I have to apply a double linear gradient but I don't understand how I can.

Could you help me?

Thank you

Beppe

Lykurg
6th May 2009, 11:19
I guess for that you have to subclass QProgressBar and draw it yourself. I can't imagine that that is possible with style sheets.
In your paint method just draw the blue gradient and then the "transparent white" one (also see QPainter::CompositionMode).

wagmare
6th May 2009, 11:25
is this one u want ....
QProgressBar::chunk {background: qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5, stop: 0 #C71585, stop: 1 white)

Lykurg
6th May 2009, 11:48
is this one u want ....
QProgressBar::chunk {background: qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5, stop: 0 #C71585, stop: 1 white)
Well, that's only a flat gradient, not a "3d simulated" one.

wagmare
6th May 2009, 11:56
oh .. means like a 3d progressbar in 2d view ..? ..

Lykurg
6th May 2009, 12:05
oh .. means like a 3d progressbar in 2d view ..? ..
With style sheet you only get that flat effect:
http://www.qtcentre.org/forum/attachment.php?attachmentid=3220&d=1241604162

But if you paint it yourself you could archeive a sort of 3d look, note the little shiny white on top and bottom in that kde 4 oxygen example
http://www.qtcentre.org/forum/attachment.php?attachmentid=3219&d=1241604162
This you can't do with style sheets.

Beppe
8th May 2009, 15:41
Thank you guys.

I implemented the paint event of my class

this is the code



void myProgressBar::paintEvent(QPaintEvent *pe)
{
Q_UNUSED(pe);

QRect pb_rect = this->geometry();
QColor pb_color;
QColor m_shadow;
QColor gradcolorH1;
QColor gradcolorH2;
int value;
int max;

int margin = 0;

int altezza_nor;
max = this->maximum();
value = this->value();
altezza_nor = (value*pb_rect.height())/max;

QPainter painter(this);

painter.setRenderHint(QPainter::Antialiasing);

m_shadow = QColor(19, 79, 236);
pb_color = QColor(139, 213, 252);
gradcolorH1 = m_shadow;
gradcolorH2 = pb_color;

//outline
painter.setPen(QPen(QBrush(Qt::black), pb_rect.width()));
QPainterPath outline;
outline.addRoundRect(0, 0, pb_rect.width(), pb_rect.height(), 0, 0);
painter.setOpacity(1);
painter.drawPath(outline);


// gradient
QLinearGradient gradient(margin, pb_rect.height() - altezza_nor + margin, pb_rect.width() - margin, pb_rect.height() -margin);
gradient.setColorAt(0.0, pb_color);
gradient.setColorAt(1.0, m_shadow);

QBrush brush(gradient);
painter.setBrush(brush);
painter.setPen(QPen(QBrush(pb_color), 5.0));

painter.setOpacity(1);
painter.drawRoundRect(margin, pb_rect.height() - altezza_nor + margin, pb_rect.width() - margin, pb_rect.height() -margin,0,0);//pb_rect.height() - 2, 0, 0);


// gradient
QLinearGradient gradientH(0, (pb_rect.height() - altezza_nor)/2, pb_rect.width(), (pb_rect.height() - altezza_nor)/2);

gradientH.setColorAt(0.0, gradcolorH1);
gradientH.setColorAt(0.2, gradcolorH2);
gradientH.setColorAt(0.8, gradcolorH2);
gradientH.setColorAt(1.0, gradcolorH1);
QBrush brushH(gradientH);
painter.setBrush(brushH);
painter.setOpacity(0.3);
painter.drawRoundRect(margin, pb_rect.height() - altezza_nor, pb_rect.width() - margin, pb_rect.height() -margin,0,0);//pb_rect.height() - 2, 0, 0);
}


I'm sure my code is not optimized. Any suggestion will be appreciated.

Thank you


Beppe