设置小部件背景颜色 [英] Set widget background color

查看:80
本文介绍了设置小部件背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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);

如何更改单元格背景?

推荐答案

代码:

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);

结果将是:

这篇关于设置小部件背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆