我有一个QMainWindow应用程序,包括菜单栏、中心小部件中的分割器和状态栏。在分割器的左侧是一个小部件,其中包含一些控件(列表、组合框和按钮),右侧是一个小部件,其中包含Matplotlib画布和NavigationToolBar的布局。
我能够使用QT样式表设置所有内容的各种背景颜色...除了Matplotlib侧。我尝试使用以下代码,但没有效果。我还尝试直接设置NavBar和Canvas样式表...但仍然没有效果:
我能够使用QT样式表设置所有内容的各种背景颜色...除了Matplotlib侧。我尝试使用以下代码,但没有效果。我还尝试直接设置NavBar和Canvas样式表...但仍然没有效果:
self.mplFig.setStyleSheet(".QWidget {background-color: #0ff}")
基本上我正在尝试将NavToolbar的背景颜色以及画布周围的边框与其他所有内容匹配(目前为青色,以便易于显示)...任何帮助都将不胜感激。完整代码和示例图像如下:
import sys
from PyQt4 import QtGui, QtCore
import matplotlib
matplotlib.use("qt4agg")
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
class testGUI(QtGui.QMainWindow):
def __init__(self, parent=None):
super(testGUI, self).__init__(parent)
self.buildLayout()
self.buildMenus()
self.menuBar()
self.statusBar()
## Style Sheets
self.splitter.setStyleSheet("QSplitter::handle:horizontal {background-color: #ccc}")
self.controlWidget.setStyleSheet(".QWidget {background-color: #0ff}")
menuStyle = """.QMenuBar {background-color: #0ff}
QMenuBar::item {background: transparent}
QMenuBar::item:selected {background: #8ff}"""
self.statusBar().setStyleSheet(".QStatusBar {background-color: #0ff}")
self.menuBar().setStyleSheet(menuStyle)
# .....THIS DOESN"T WORK !! .....
self.mplFig.setStyleSheet(".QWidget {background-color: #0ff}")
def buildLayout(self):
self.controlWidget = QtGui.QWidget(self)
self.plotList = QtGui.QListWidget(self)
self.combo = QtGui.QComboBox(self)
self.button = QtGui.QPushButton('Plot')
self.combo.addItems(\['1','2','3','4'\])
layout = QtGui.QVBoxLayout()
layout.addWidget(self.plotList)
layout.addWidget(self.combo)
layout.addWidget(self.button)
self.controlWidget.setLayout(layout)
self.mplFig = MplGrapher()
self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
self.splitter.addWidget(self.controlWidget)
self.splitter.addWidget(self.mplFig)
self.setCentralWidget(self.splitter)
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Plastique'))
def buildMenus(self):
openFile = QtGui.QAction('Open', self)
self.fileMenu = self.menuBar().addMenu('&File')
self.fileMenu.addAction(openFile)
class MplGrapher(QtGui.QWidget):
def __init__(self,parent=None):
super(MplGrapher, self).__init__(parent)
self.initFigure()
def initFigure(self):
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
self.navbar = NavigationToolbar(self.canvas, self)
self.figure.add_subplot(1,1,1)
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.navbar)
self.layout.addWidget(self.canvas)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = testGUI()
main.show()
sys.exit(app.exec_())