PDA

View Full Version : [RESOLVED] Problems chaning background color of QTableWidgetItem



Sarol
15th June 2011, 17:20
Good Day Everyone,

I just upgraded to QT 4 this week, and still learning some of the features.

Currently my problem is changing the background color of a QTalbeWidgetItem

Here is what I am doing:

dataTable->itemat( row , col ) -> setBackground ( QBrush( Qt::cyan) ) ;

(where dataTable is a QTableWidget)

The good news is a cell does appear in cyan, however it is the wrong cell, and it's always the cell at 0 , 0 no matter what row , col I use.

If somebody could help me figure out what I'm doing wrong it would be greatly appreciated.

Let me know if more info is needed on this problem.

Thank you,

Sarol

Sarol
15th June 2011, 19:59
I figured out how to do what I needed but still not sure why the above method does not work. Here is I changed:

Original Code:
dataTable->itemat( row , col ) -> setBackground ( QBrush( Qt::cyan) ) ;

New Code:
dataTable->item( row , col ) ->setData( Qt::BackgroundRole , QBrush( Qt::cyan ) ) ;

And now its all working...

wysota
15th June 2011, 21:44
The error must have been elsewhere since this is the implementation of setBackground:

inline void setBackground(const QBrush &brush) { setData(Qt::BackgroundRole, brush); }

Your row and col must have had wrong values.

Sarol
16th June 2011, 15:48
well after looking at my original code I found what I was doing wrong.

I was doing:
dataTable->itemat( row , col ) -> setBackground ( QBrush( Qt::cyan) ) ;

When I should have been doing:
dataTable->item( row , col ) -> setBackground ( QBrush( Qt::cyan) ) ;

Now on to other issues. :)