Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How change the background color for a blank cell in QTableWidget

If a cell have some data, using

tableWidget->item(8,0)->setBackgroundColor(Qt::red); 

to change the background color will work, but if a cell is blank it will fail.

like image 488
spy8888 Avatar asked Mar 25 '13 15:03

spy8888


People also ask

How to change QTableWidget background color?

tableWidget->item(8,0)->setBackgroundColor(Qt::red); to change the background color will work, but if a cell is blank it will fail.

Which command is used to make the cell background Colourful in Excel?

Select the cell or range of cells you want to format. Click Home > Format Cells dialog launcher, or press Ctrl+Shift+F. On the Fill tab, under Background Color, pick the color you want. To use a pattern with two colors, pick a color in the Pattern Color box, and then pick a pattern in the Pattern Style box.


1 Answers

You cannot set the background color of a cell unless it contains a QTableWidgetItem (as the background color is a property of the item).

So you need to populate your QTableWidget with empty items first. In your example, create the item before you attempt to set the background color.

tableWidget->setItem(8, 0, new QTableWidgetItem); tableWidget->item(8, 0)->setBackground(Qt::red); 

Please also note that you should use setBackground instead of setBackgroundColor as the latter is deprecated.

like image 165
Daniel Hedberg Avatar answered Sep 18 '22 20:09

Daniel Hedberg