在幫助中搜索qt style sheets
找到這句話:
If we want the property to apply only to one specific QLineEdit, we can give it a name using QObject::setObjectName() and use an ID Selector to refer to it:
意思是如果不想子控件繼承父控件,就在父控件加#
if(m_MainWidget.objectName().isEmpty()){ m_MainWidget.setObjectName("backLineEdit"); } m_MainWidget.setStyleSheet("QWidget#backLineEdit{border-image:url(:/image/background.jpg);}");
m_MainWidget是父控件,先setObjectName一個名,隨便起一個,然后#加上這個名字。這樣子控件就不會繼承父控件的樣式了。