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.
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.
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.Please also note that you should use
setBackground
instead ofsetBackgroundColor
as the latter is deprecated.