python - 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,但没有成功。
解决方案
推荐阅读
- php - Laravel 8 没有从包“\Conner\Tagging\Taggable;”中找到“标签”
- c - 时间复杂度会根据参数变化吗?
- reactjs - 开玩笑的期望语句不会等待承诺得到解决
- strapi - 如何在strapi中使用根路由(/)作为/admin
- google-sheets - 从 vlookup 检查 Google 表格中的文本而不破坏公式、文本换行或将列宽更改为“适合数据”?
- javafx - 结束时如何重置 JavaFX 媒体播放器?
- java - 导航 kotlin java 后,片段 recyclerview 消失
- matlab - 如何在matlab中向等高线图添加约束
- json - 是否可以同时使用 Spring Boot 应用程序来控制网站和作为 API?
- windows - Windows Server Web Application Load Balancer - 如何检查它是使用粘性会话还是循环?