Cant set the background color of a QTableWidgetItem?

817 views Asked by At

I am trying to set the background color of a QTableWidgetItem, but it doenst work? When I try to change the textcolor, it works without any problems..

Code:

void myWin::myFunction(int count, QVector<QColor> sampleInfoColor, QVector<QStringList> info)
{
    ...
    sampleTable->setRowCount(count + 1);
    QFont nameFont;
    nameFont.setBold(true);
    ...
    for(int i=0; i<count; i++)
    {
        ...
        QTableWidgetItem *name = new QTableWidgetItem("Text");
        name->setFont(nameFont);
        sampleTable->setItem(i+1, 0, name);
        name->setBackground(sampleInfoColor[i]);
        ...
    }
}

I also tried this, but it didnt work too..:

sampleTable.item(nRow, nCol)->setBackground(QColor);

What am I doing wrong?

2

There are 2 answers

1
PRIME On

Can you give this a try:

name->setBackground( QBrush( sampleInfoColor[i] ) );
0
DC大锤 On

Put Label into tableWidget, modify the color of Label, label is QLabel

tableWidget->setItem(row, col, new QTableWidgetItem);
tableWidget->setCellWidget(row, col++, label);
label->setStyleSheet("background-color: rgb(0, 229, 0);");