Stylesheet for nested custom widget not applied

混江龙づ霸主 提交于 2019-12-23 22:14:57

问题


I expect the following code to show a small black area inside outer main window:

class Canvas(QWidget):
    pass

app = QApplication(sys.argv)
outer = QWidget()
w = Canvas(outer)
w.setStyleSheet("background-color: black")
outer.show()

But looks like the stylesheet is not applied: entire outer window is gray. However, if w is a QWidget, code works as expected. When Canvas instance is shown directly (without parent) stylesheet is properly applied as well:

w = Canvas()
w.setStyleSheet("background-color: black")
w.show()

This code shows a black window. I have tried same in C++ with Qt 4.8.6 and stylesheet is also properly applied for nested widgets derived from QWidget.

What am I missing? I am using python 2.7.6, Qt 4.8.6 and PyQt 4.10.4 under Ubuntu 14.04.


回答1:


A QWidget subclass will ignore stylesheets by default (for performance reasons).

Try this:

w = Canvas(outer)
w.setAttribute(QtCore.Qt.WA_StyledBackground)
w.setStyleSheet("background-color: black")


来源:https://stackoverflow.com/questions/29504589/stylesheet-for-nested-custom-widget-not-applied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!