设置布局的背景色 [英] Set background color of layout

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

问题描述

由于布局对象没有属性".setStyleSheet()",如何为给定布局设置背景颜色?

As the layout object has no attribute ".setStyleSheet()", how can one set the background color for a given layout?

作为直观的解释,我可以设置标签和按钮的背景色,但不能设置包括分隔符的整个布局.

As a visual explanation, I can set both the label and the button background color, but not the entire layout, which includes the spacer.

以编程方式,我以水平布局组织一些信息并将其显示在框架中.我想为每个循环替换背景颜色.

Programatically, I'm organizing some information in horizontal layouts and displaying them in a frame. I would like to alternate background colors for each loop.

for param_name in parameters:
    hlayouts.append(QtGui.QHBoxLayout())
    labels.append(QtGui.QLabel("%s"%param_name))
    sliders.append(QtGui.QSpacerItem(10,10,hPolicy=QtGui.QSizePolicy.Expanding))
    spins.append(QtGui.QDoubleSpinBox())

    spins[index].setValue(float(values.get(param_name)))
    labels[index].setStyleSheet("background-color:black;")
    spins[index].setStyleSheet("background-color:black;")

    hlayouts[index].addWidget(labels[index])
    hlayouts[index].addItem(sliders[index])
    hlayouts[index].addWidget(spins[index])

    index += 1

vlayout = QtGui.QVBoxLayout()
for i in range(len(hlayouts)):
    vlayout.addLayout(hlayouts[i])

推荐答案

您可以在一个空的QWidget上添加设置布局,然后在此Widget上设置StyleSheet.

You could just add set the layout on an empty QWidget and set the StyleSheet on this widget.

for index, param_name in enumerate(parameters):
    container = QtGui.QWidget(self)
    layout = QtGui.QHBoxLayout(container )

    hlayouts.append(container)
    labels.append(QtGui.QLabel("%s"%param_name))
    sliders.append(QtGui.QSpacerItem(10,10,hPolicy=QtGui.QSizePolicy.Expanding))
    spins.append(QtGui.QDoubleSpinBox())

    spins[index].setValue(float(values.get(param_name)))
    container.setStyleSheet("background-color:black;")

    layout.addWidget(labels[index])
    layout.addItem(sliders[index])
    layout.addWidget(spins[index])


vlayout = QtGui.QVBoxLayout(self)
for widget in hlayouts:
    vlayout.addWidget(widget)

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

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