QT5.2 + VS2012 给QWidget 对象使用setStyleSheet()设置背景图片运行不显示的问题

            使用stylesheet设置背景图片,如果是在mainwindowdialog中,直接右键change style sheet在add resource中选择background-image或者border-image,选择资源文件中的图片就行了。

             但在widget中,使用stylesheet(),在qt designer虽然显示有图片。

没设置widgetstylesheet()时,如图:

QT5.2 + VS2012 给QWidget 对象使用setStyleSheet()设置背景图片运行不显示的问题


qt designer如图:

           、QT5.2 + VS2012 给QWidget 对象使用setStyleSheet()设置背景图片运行不显示的问题


运行后:QT5.2 + VS2012 给QWidget 对象使用setStyleSheet()设置背景图片运行不显示的问题


总结:  当weight 没有父窗体时,如果给它设置setStyleSheet(),它不会显示背景图片,反而它的子窗体,像图中的

pushbutton, label等的StyleSheet()中的图片就从weight继承而来(ps:并没有设置pushbutton, label的StyleSheet)



为什么mainwindowdialog可以直接设置,而widget不行呢?

如图:

QT5.2 + VS2012 给QWidget 对象使用setStyleSheet()设置背景图片运行不显示的问题


:由上图可知,qt三大窗体部件 mainwindow, dialog, weight之间的关系,mainwindow, dialog继承于weight,而

weight 没有父窗体。