2016-09-23 35 views
0

我看到我不是the only one在pyqtgraph中有一个背景颜色问题 - 我正在编写一个QGIS软件插件,它带有一个带有图形的附加对话框。我试图设置背景颜色,并且只在使用QGIS Plugin Reloader插件重新加载插件之后才加载(这个插件是为开发插件的人制作的,所以在代码发生任何更改后,刷新并加载一个新插件到QGIS中,它不被普通用户使用)。pyqtgraph - 仅在重新加载后才加载背景颜色

我的下面这段代码:

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

有趣的是,没有任何问题,(即使前景色!)所有buildGraph()负荷的零件只有背景颜色不会。

这是一个已知的错误还是设置前景色和背景色有区别?链接的问题并没有帮助我解决这个问题。

pyqtgraph==0.9.10 PyQt4==4.11.4 Python 2.7.3

回答

0

pyqtgraph documentation说,大约setConfigOption设置,即:

注意,这必须在创建任何部件

在我的代码之前设置我有

def buildGraph(self): 

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

    barGraph = self.graph.graphicsView 

这就是我认为的“之前”的地方,但它是创建一个对象,而不是小部件。一个人应该在课内写setConfigOption,即负责存储pyqtgraph对象。在我的情况下,它是一个单独的文件创建一个单独的对话框内__init__功能:

从PyQt4的进口QtGui,QtCore 从plugin4_plot_widget进口Ui_Dialog 从plugin4_dialog进口plugin4Dialog 进口pyqtgraph

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