pyqtgraph - Background color loads only after reloading
<p>I see I'm not <a href="https://stackoverflow.com/questions/31567573/issue-in-setting-the-background-color-in-pyqtgraph">the only one</a> having a problem with background color in pyqtgraph - I'm writing a QGIS software plugin which has an additional dialog box with a graph. I'm trying to set the background color and it loads only after I reload the plugin using QGIS Plugin Reloader plugin (<em>this plugin is made for people developing plugins, so after any change in the code, you refresh and have a new one loaded into QGIS. It is not used by a common user</em>).  </p>

<p>My piece of code below:</p>

<pre><code>import pyqtgraph

...

def prepareGraph(self): # loads on button click

    self.graphTitle = 'Graph one'

    # prepare data - simplified, but data display correctly
    self.y = something
    self.x = something_else

    self.buildGraph() 


def buildGraph(self):
    """ Add data to the graph """
    pyqtgraph.setConfigOption('background', (230,230,230))
    pyqtgraph.setConfigOption('foreground', (100,100,100))
    dataColor = (102,178,255)
    dataBorderColor = (180,220,255)
    barGraph = self.graph.graphicsView
    barGraph.clear()
    barGraph.addItem(pyqtgraph.BarGraphItem(x=range(len(self.x)), height=self.y, width=0.5, brush=dataColor, pen=dataBorderColor))
    barGraph.addItem(pyqtgraph.GridItem())
    barGraph.getAxis('bottom').setTicks([self.x])
    barGraph.setTitle(title=self.graphTitle)

    self.showGraph()


def showGraph(self):
    self.graph.show()
</code></pre>

<p>Interesting thing is that <strong>all parts of <code>buildGraph()</code> load without any issue, (even the foreground color!)</strong> only the background color won't. </p>

<p>Is this a known bug or there is a difference between setting fore- and background color? Linked question didn't help me to solve this problem.</p>

<p><code>pyqtgraph==0.9.10
PyQt4==4.11.4
Python 2.7.3</code></p>

<p>The <a href="http://www.pyqtgraph.org/documentation/style.html#default-background-and-foreground-colors" rel="nofollow">pyqtgraph documentation</a> says about <code>setConfigOption</code> settings, that:</p>

<blockquote>
  <p>Note that this must be set before creating any widgets</p>
</blockquote>

<p>In my code I have</p>

<pre><code>def buildGraph(self):

    pyqtgraph.setConfigOption('background', (230,230,230))
    pyqtgraph.setConfigOption('foreground', (100,100,100))

    barGraph = self.graph.graphicsView
</code></pre>

<p>and that's what I thought is the "before" place, but it's creation of an object, not widget. One should write <code>setConfigOption</code> inside a class, that is responsible for storing <code>pyqtgraph</code> object. In my case it was <code>__init__</code> function inside a separate file creating a separate dialog box:</p>

<p>from PyQt4 import QtGui, QtCore
from plugin4_plot_widget import Ui_Dialog
from plugin4_dialog import plugin4Dialog
import pyqtgraph</p>

<pre><code>class Graph(QtGui.QDialog, Ui_Dialog):
    def __init__(self):
        super(Graph, self).__init__()
        pyqtgraph.setConfigOption('background', (230,230,230))
        pyqtgraph.setConfigOption('foreground', (100,100,100))        

    self.setupUi(self)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    main = Graph()
    main.show()
    sys.exit(app.exec_())
</code></pre>

python qgis pyqtgraph