Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set widget background color

I use QCheckBox in QTableWidgetCell

QWidget *widget = new QWidget();
QCheckBox *checkBox = new QCheckBox();
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(checkBox);
layout->setAlignment(Qt::AlignCenter);
layout->setContentsMargins(0, 0, 0, 0);
widget->setLayout(layout);
table->setCellWidget(0, 0, widget);

How can I change cell background?

like image 691
Ufx Avatar asked Oct 09 '14 19:10

Ufx


People also ask

How do I add background color to WordPress widget?

To access the WordPress Theme Customizer, you can log in to your website and then go to Appearance » Customize. This will open the Theme Customizer, where you'll find multiple options to modify your theme. This includes the menus, colors, homepage, widgets, background image, and more.

How do I change the background color of Qframe?

ui->frame->setStyleSheet(""); ui->frame->setStyleSheet("background-color: rgb(255,255,255)");


3 Answers

The code:

widget->setStyleSheet("background-color: red");

works fine but you need to set the style for every container widget you add to your table:

So in order to see the change you need the following code:

QWidget *widget = new QWidget();
widget->setStyleSheet("background-color: red");
QCheckBox *checkBox = new QCheckBox();
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(checkBox);
layout->setAlignment(Qt::AlignCenter);
layout->setContentsMargins(0, 0, 0, 0);
widget->setLayout(layout);

QWidget *widget2 = new QWidget();
widget2->setStyleSheet("background-color: red");
QCheckBox *checkBox2 = new QCheckBox();
QHBoxLayout *layout2 = new QHBoxLayout(widget2);
layout2->addWidget(checkBox2);
layout2->setAlignment(Qt::AlignCenter);
layout2->setContentsMargins(0, 0, 0, 0);
widget2->setLayout(layout);

ui->tableWidget->setCellWidget(0, 0, widget);
ui->tableWidget->setCellWidget(0, 1, widget2);

And the result will be:

enter image description here

like image 151
Jacob Krieg Avatar answered Oct 16 '22 20:10

Jacob Krieg


You should try this:

checkBox->setStyleSheet("background-color: red;");

If you want to specify it more generally, write the classtype in the CSS to indicate which class in the hierarchy should handle the flag. This could look something like this then:

QWidget { background-color: red; }
like image 1
msrd0 Avatar answered Oct 16 '22 20:10

msrd0


If you want to change cell background, not a widget, use setBackground() method:

QCheckBox *checkBox = new QCheckBox("example");
QWidget *widget = new QWidget();
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(checkBox);
layout->setAlignment(Qt::AlignCenter);
layout->setContentsMargins(0, 0, 0, 0);
widget->setLayout(layout);
ui->tableWidget_2->setCellWidget(0,0,widget);
ui->tableWidget_2->item(0, 0)->setBackground(Qt::red);//this line should be

In this case all your cell will be red (without white lines around checkbox).

like image 1
Kosovan Avatar answered Oct 16 '22 21:10

Kosovan