PDA

View Full Version : Add Gradient to Background for QListView



vasant
17th February 2010, 11:44
Hi,

I want to setbackground of Rows in QListView with Gradient.I am attaching the way i want,Pls have a look.
I want exactly what we see in the qtcentre site.white background and green text on it.How can i do that?.Please help me.

Attachment:http://www.qtcentre.org/forums/2-Qt-Programming

Thank you,

Lykurg
17th February 2010, 12:03
If you have a view, then just use the delegate and paint the background yourself using the paint method. Use QLinearGradient as a brush for your painter.

vasant
18th February 2010, 12:19
Thanks for the reply, Can you guide me bit more.....as i am new to QT.I have gone through documentation.

BalaQT
18th February 2010, 12:21
vasant
no attachment found.
pls attach again

Bala

BalaQT
18th February 2010, 12:24
hi

for gradient use this stylesheet
for blue color

t->setStyleSheet("background-color: qlineargradient(x1: 0.5, y1: 0.5 x2: 0.5, y2: 1, stop: 0 #6666FF, stop: 0.7 #99CCFF );");

for green color

t->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0 x2: 0, y2: 1, stop: 0 #00CD00 , stop: 0.2 #0AC92B stop: 1 #00FF33 );");

not exactly as qtcenter. but its a green gradient

hope it helps
Bala

vasant
18th February 2010, 12:53
Thanks Bala for the reply,I am able to draw single color.But,How can i draw gradient to the Row Items of ListView?.

BalaQT
18th February 2010, 13:49
have u tried the mentioned stylesheet?


t->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0 x2: 0, y2: 1, stop: 0 #00CD00 , stop: 0.2 #0AC92B stop: 1 #00FF33 );");

its not single color, it contains three colors.
to change different color change the hex code in stop: 0 #00CD00

just copy and paste the stylesheet. its a green gradient with three diff green color

Try and give ur feedback

Bala

been_1990
18th February 2010, 13:57
If you are using QtDesigner, right-click your QListView and select "Change styleSheet...", the select "Add Gradient" -> "background-color".

vasant
19th February 2010, 07:27
I am getting total background as gradient.But i want only Rows with Gradient effect as we will see in QTcentre home page.

aamer4yu
19th February 2010, 07:37
I am getting total background as gradient.But i want only Rows with Gradient effect as we will see in QTcentre home page.

For that you will need to use delegates. In the paint function of delegate, fill the painter rect with the linear gradient you want.
Then call the base class function to draw over it.

HINT : You need to draw background only for root items :)

vasant
19th February 2010, 08:01
Yeah,i have done that . But i am confused how to paint particular index of ListView.I have gone through the examples in demos.but not able to get how to get the index of ListView.Kindly throw some light on this.

aamer4yu
19th February 2010, 08:08
What did you try ? You get the index in paint function of the delegate.
QStyledItemDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index )