如何在Qt中设置QTableWidget背景透明?

5

我正在开发一个应用程序,其中使用了QTableWidgets,并且需要将其背景设置为透明。我已经尝试从表单中使用setStyleSheet "background:transparent;",但是没有任何反应,是否有其他方法可以实现? 我贴出了一张截图。

2个回答

14

你走在正确的道路上。尝试一下:

setStyleSheet("QTableWidget {background-color: transparent;}"
              "QHeaderView::section {background-color: transparent;}"
              "QHeaderView {background-color: transparent;}"
              "QTableCornerButton::section {background-color: transparent;}");
QTableWidget *table = new QTableWidget(latticeView);
table->setRowCount(2);
table->setColumnCount(2);
注意,我是在创建表格小部件之前设置样式表的。我不知道为什么,但这似乎是必要的。

0

在您的样式表上使用background-color: rgba(0,0,0,0);,可以得到透明的表格。此外,通过改变Alpha通道,您可以为您的表格带来很好的效果。

例如:background-color: rgba(0,0,0,50);

QTableWidget
{
    background-color: rgba(0,0,0,50);
    color: rgb(255, 255, 255);
    font-size:22px;
}

QTableWidget::item
{
    color: rgb(255, 255, 255);
    background-color:rgba(0,0,0,150);
    text-align:center;
}

QTableWidget::item:hover
{
    color:#FFFFFF;
    background: #4B4B4D;
}

QTableWidget::item:selected
{
    color:#FFFFFF;
    background: #4B4B4D;
}

QHeaderView{
    background-color: rgba(0,0,0,70);
}

QHeaderView::section,QTableCornerButton:section
{
    text-align:center;
    padding:3px;
    margin:0px;
    color: rgb(255, 255, 255);
    background-color: rgba(0,0,0,70);
    border:1px solid #242424;
    border-radius: 8px;
}

QHeaderView::section:selected
{
    color:#FFFFFF;
    border:1px solid #242424;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接