QTableWidget - How to set background-color with widget
-
Hello,
I try to set a background color to my QTableWidget (particular celle or row) with setBackgroundColor.
This function works with QTableWidgetItem but crashes with others items like QCheckBox are contained in the gridIs there any other function to set the color in a cell/row ?
I've already tried *setstylesheet *function, which crashes too...
-
Hi,
Can you share the code you use to setup the background color and widgets ?
-
yes of course :
[...]
tableItem = new QTableWidgetItem("");
tableItem->setFlags(tableItem->flags() & ~(Qt::ItemIsEditable));
//Add Table items here With Default Cell Texts
ui->tableWidget->setItem(y,0,new QTableWidgetItem(""));
ui->tableWidget->setItem(y,1,tableItem);
ui->tableWidget->setItem(y,2,new QTableWidgetItem(""));
ui->tableWidget->setCellWidget(y,3,new QCheckBox(""));
ui->tableWidget->setCellWidget(y,4,new QCheckBox(""));
ui->tableWidget->setCellWidget(y,5,new QCheckBox(""));
[...]The background-color works only for "QTableWidgetItem" cells (without widget)
-
When you set a cell widget you don't see the item underneath (and you don't have any under), you have to set the background on the widget itself.
-
is there any global function to set the background-color ? (which works with or without widget...) ?
-
You can try by modifying the palette of the application.
-
I want to modify a specific TableWidget and not all grids or all widget contained in the application...Have you an example ?
-
You want to modify only that widget and all its children ?
-
I want to modify all cells of QTableWidget, several cells contain QTableWidgetItem, several cells contain QCheckbox for example
-
Then you can write the style sheet so that it applies to both your QTableWidget and its children widgets
-
It seems to work on QTableWidget but not on children widgets when I set the style sheet
-
I have resolved my problem with :
ui->tableWidget->item(i,j)->setBackground(Qt::gray);