首页 > 解决方案 > Qt 设计器:图形查看和绘制图形

问题描述

我想知道如何在 QtWidgets.QGraphicsView 中显示一个简单的绘图图。

这是我的代码

from PyQt5 import QtCore, QtGui, QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import numpy as np
import matplotlib.pyplot as plt


class Ui_scene(object):
    def setupUi(self, scene):
        scene.setObjectName("scene")
        scene.resize(1109, 881)
        self.graphicsView = QtWidgets.QGraphicsView(scene)
        self.graphicsView.setGeometry(QtCore.QRect(30, 20, 461, 371))
        self.graphicsView.setObjectName("graphicsView")
        self.graphicsView_2 = QtWidgets.QGraphicsView(scene)
        self.graphicsView_2.setGeometry(QtCore.QRect(30, 400, 461, 371))
        self.graphicsView_2.setObjectName("graphicsView_2")
        self.graphicsView_3 = QtWidgets.QGraphicsView(scene)
        self.graphicsView_3.setGeometry(QtCore.QRect(520, 20, 461, 371))
        self.graphicsView_3.setObjectName("graphicsView_3")
        self.pushButton = QtWidgets.QPushButton(scene,clicked = lambda: self.graph1())
        self.pushButton.setGeometry(QtCore.QRect(720, 570, 75, 23))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(scene)
        QtCore.QMetaObject.connectSlotsByName(scene)
        
    def graph1(self):
       
        figure=Figure()
        axes=figure.gca()
        
        t = np.arange(0.0, 2.0, 0.01)
        s = 1 + np.sin(2 * np.pi * t)
        
        axes.set_title("title")
        axes.plot(t,s)
        canvas = FigureCanvas(figure)
        canvas.setGeometry(0, 0, 500, 500)
        
        print("graph1")
        self.graphicsView.setScene()
        
        
  
    def retranslateUi(self, scene):
        _translate = QtCore.QCoreApplication.translate
        scene.setWindowTitle(_translate("scene", "Form"))
        self.pushButton.setText(_translate("scene", "PushButton"))




if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    scene = QtWidgets.QWidget()
    ui = Ui_scene()
    ui.setupUi(scene)
    scene.show()
    sys.exit(app.exec_())

如您所见,我创建了三个图形查看器,其想法是在每个图形上显示一个图。问题是我无法链接 Qtwidgets 和数字。

我正在使用 qt 5.9.7 并且此版本不支持 pyqtgraph。我更努力地更新到 qt 5.12,但没有成功。

标签: pythonmatplotlibpyqt5qt-designer

解决方案


推荐阅读