0

I am trying to add a label with color palette, to a QTableWidget. But its not displayed with the color i set to label palette. here is my code,

            QWidget *colorTableWidget = new QWidget();
            QLabel *lbl = new QLabel();
            lbl->setAutoFillBackground(true);

            QPalette palette;
            palette.setColor(lbl->backgroundRole(), QColor("RED") );
            lbl->setPalette(palette);

            QGridLayout *gridLayout = new QGridLayout();
            gridLayout ->addWidget(lbl,0,0);

            colorTableWidget->setLayout(gridLayout);

            ui->tableWidget->setCellWidget(row,0,colorTableWidget);

could any one tell me what is wrong here.. ?, same thing is working when I add a text to the label but its not working for color palette. I am using Qt 4.8.4 in win 7 environment.

Prady
  • 663
  • 3
  • 11
  • 28

1 Answers1

0

The purpose of having the grid layout in your code is not clear to me. I would re-write your code in the following, much simplified, way:

QLabel *lbl = new QLabel;
lbl->setAutoFillBackground(true);

QPalette palette = lbl->palette();
palette.setColor(lbl->backgroundRole(), Qt::red );
lbl->setPalette(palette);

ui->tableWidget->setCellWidget(row, 0, lbl);
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • I need to have the grid layout to have multiple labels with color palette, but It looks better question now. thanks – Prady Oct 24 '13 at 11:10