我有一个带有网格布局的主窗口
类Ui_MainWindow(对象):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1850, 1700) # width, height
self.gl_main = QGridLayout(self.centralwidget)
w = ChartWidget(self)
self.gl_main.addWidget(w, 4, 0, 1, 1)
class ChartWidget(QWidget):
def __init__(self, parent=None):
super(ChartWidget, self).__init__(parent)
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.axes = self.figure.add_subplot(111)
self.axes.hold(False)
self.axes.plot(range(10), range(10), 'b')
self.canvas.draw()
self.show()
但是,chartwidget不会添加到gridlayout中。我可以简单地看到它在屏幕上闪烁,但随后就消失了。我该如何附上它?
答案 0 :(得分:0)
将画布添加到gridlayout而不是小部件解决了这个问题。
w = ChartWidget(self)
self.gl_main.addWidget(w.canvas, 4, 0, 1, 1)