PyQt:向布局添加多个小部件

时间:2018-03-06 18:19:23

标签: python pyqt pyqtgraph

我正在尝试使用Pyqt设置图形视图。我有一个垂直框布局,我想在其中添加一个GLViewWidget对象,然后添加一个GraphicsLayoutWidget。但是当我将这些小部件添加到垂直框布局时,GraphicsLayoutWidget接管整个布局,我无法看到GLViewWidget对象。我已经包含了以下代码。

class view_manager(QtCore.QObject):
       def __init__(self):
            super(view_manager, self).__init__()
            self._layout = QtGui.QVBoxLayout()

            self._detectorView = viewport()
            self._layout.addWidget(self._detectorView)

            b = QtGui.QPushButton("HEY")
            self._layout.addWidget(b)

            self._plotView = self.getPlot()
            self._layout.addWidget(self._plotView)

    def getLayout(self):
            return self._layout

    def getPlot(self):
            plot_widget = pg.GraphicsLayoutWidget()

            #plot_widget.resize(800,350)
            plt1 = plot_widget.addPlot()
            plt2 = plot_widget.addPlot()

            ## make interesting distribution of values
            vals = numpy.hstack([numpy.random.normal(size=500), numpy.random.normal(size=260, loc=4)])

            ## compute standard histogram
            y,x = numpy.histogram(vals, bins=numpy.linspace(-3, 8, 40))

            ## Using stepMode=True causes the plot to draw two lines for each sample.
            ## notice that len(x) == len(y)+1
            plt1.plot(x, y, stepMode=True, fillLevel=0, brush=(0,0,255,150))

            ## Now draw all points as a nicely-spaced scatter plot
            y = pg.pseudoScatter(vals, spacing=0.15)
            #plt2.plot(vals, y, pen=None, symbol='o', symbolSize=5)
            plt2.plot(vals, y, pen=None, symbol='o', symbolSize=5, symbolPen=(255,255,255,200), symbolBrush=(0,0,255,150))

            plot_widget.show()
            return plot_widget

detectorView对象继承自GLViewWidget。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

经过广泛搜索,我找到了解决方案。致电setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) 从我的GLViewWidget修复了这个问题。