How to set PyQT4 stylesheet for Matplotlib widget (navBar / canvas)?

I have a QMainWindow application consisting of a menu bar, a Splitter in the center widget and a status bar. On the left side of the divider there is a widget containing some controls (list, combo and button), and on the right is a widget containing the Matplotlib canvas layout and NavigationToolBar.

I was able to use QT styles to set different background colors for everything ... except the Matplotlib side. I tried using the following, but it has no effect. I also tried to set the NavBar and Canvas stylesheets directly ... but again that didn't work:

self.mplFig.setStyleSheet(".QWidget {background-color:   #0ff}")

      

Basically I'm trying to rotate the background color of the NavToolbar and the border around the canvas to match everyone else (currently blue so it's easy to get it) ... Any help would be appreciated. Full code and sample image below:

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_())

      

Sample GUI

+3


source to share


1 answer


I think I may have a solution thanks to some advice from Maxwell Grady.

I changed the following two lines:

self.mplFig.setStyleSheet("QWidget {background-color:   #0ff}")

      

Note the absence of a "." before QWidget and



class MplGrapher(QtGui.QGroupBox):

      

The image of the graphical interface after the changes. Now that I can set some style properties ... it's time to make it less ugly.

Sample with colored in borders.

+1


source







All Articles