PyQT6 (4)QGraphicsView 显示图片

PyQT6 (4)QGraphicsView 显示图片

GraphicsView使用

from PyQt5.QtGui import QImage, QPixmap from PyQt5.QtWidgets import QGraphicsPixmapItem, QGraphicsScene def showImage(self): frame = QImage("img/entrance1.png") pix = QPixmap.fromImage(frame) item = QGraphicsPixmapItem(pix) scene = QGraphicsScene() scene.addItem(item) self.graphicsView.setScene(scene)

QGraphicsScene使用

class SeisPro(QtWidgets.QMainWindow, Ui_SeismicProgramme): def __init__(self, parent=None): super(SeisPro, self).__init__(parent) # 设置窗体 self.setupUi(self) # 输入框默认值设置 self.time_txt.setPlainText("0") self.velocity_txt.setPlainText("0") self.distance_txt.setPlainText("0") # 0. 绘图控件,创建一个QGraphicsScene self.velocity_view.setScene(QGraphicsScene()) # 按钮 self.oky_btn.clicked.connect(self.oky_btn_func) self.cancel_btn.clicked.connect(self.cancel_btn_fnc) # 查询数据 def oky_btn_func(self): self.distance_txt.setPlainText("1") self.add_velocitypic() # self.add_distancepic() self.plt_distancepic() def plt_distancepic(self): # 1. 创建场景 scene = QGraphicsScene() # 2. 绘制内容 # Draw a rectangle item, setting the dimensions. rect = QGraphicsRectItem(0, 0, 200, 50) # Set the origin (position) of the rectangle in the scene. rect.setPos(50, 20) # Define the brush (fill). brush = QBrush(Qt.GlobalColor.red) rect.setBrush(brush) # Define the pen (line) pen = QPen(Qt.GlobalColor.cyan) pen.setWidth(10) rect.setPen(pen) # 3. 加载内容到场景中 scene.addItem(rect) # 4. 将场景添加至视图 self.distance_view.setScene(scene)

示例

class SeisPro(QtWidgets.QMainWindow, Ui_SeismicProgramme): def __init__(self, parent=None): super(SeisPro, self).__init__(parent) # 设置窗体 self.setupUi(self) # 输入框默认值设置 self.timeLineEdit.setText('0') # self.timeLineEdit.setPlainText("0") self.velocityLineEdit.setText("0") self.distanceLineEdit.setText("0") # 绘图控件,创建一个QGraphicsScene self.velocity_view.setScene(QGraphicsScene()) # 按钮 self.oky_btn.clicked.connect(self.oky_btn_func) self.cancel_btn.clicked.connect(self.cancel_btn_fnc) def add_velocitypic(self): frame = QImage("person.jpg") pix = QPixmap.fromImage(frame) pix = pix.scaled(200, 260) # 调整图片大小為 200x260 item = QGraphicsPixmapItem(pix) scene = QGraphicsScene() # QGraphicsScene 相對位置 (20, 20) 和大小 200x260 scene.setSceneRect(0, 0, 200, 260) scene.addItem(item) self.velocity_view.setScene(scene)
 
 

图片自适应窗口的大小

  • 这样存在窗口最大化时,图片不变化
class SeisPro(QtWidgets.QMainWindow, Ui_SeismicProgramme): def __init__(self, parent=None): super(SeisPro, self).__init__(parent) # 设置窗体 self.setupUi(self) # 输入框默认值设置 self.timeLineEdit.setText('0') # self.timeLineEdit.setPlainText("0") self.velocityLineEdit.setText("0") self.distanceLineEdit.setText("0") # 绘图控件,创建一个QGraphicsScene self.velocity_view.setScene(QGraphicsScene()) self.distance_view.setScene(QGraphicsScene()) # 按钮 self.oky_btn.clicked.connect(self.oky_btn_func) self.cancel_btn.clicked.connect(self.cancel_btn_fnc) def oky_btn_func(self): # 查询数据 self.distanceLineEdit.setText("1") self.add_velocitypic() self.add_distancepic() def contour_fnc(self, x, y): # 绘制等值线函数 return (1 - y**5 + x**5) * np.exp(-x**2 - y**2) def add_velocitypic(self): # 加载速度图形 frame = QImage("person.jpg") pix = QPixmap.fromImage(frame) width = self.velocity_view.width() height = self.velocity_view.height() # print(width,height) pix = pix.scaled(width, height) # 调整图片大小為 200x260 item = QGraphicsPixmapItem(pix) scene = QGraphicsScene() # QGraphicsScene 相對位置 (20, 20) 和大小 200x260 scene.setSceneRect(0, 0, width, height) #设置原点和scene大小 scene.addItem(item) self.velocity_view.setScene(scene) def main(): app = QApplication(sys.argv) pr = SeisPro() pr.show() app.exec() if __name__ == '__main__': main()
进一步修改
''' Description: henggao_note version: v1.0.0 Date: 2023-06-19 20:34:33 LastEditors: henggao LastEditTime: 2023-06-26 08:49:25 ''' import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import Qt,QRectF from PyQt5.QtGui import (QBrush, QColor, QImage, QPainter, QPainterPath, QPen, QPixmap) from PyQt5.QtWidgets import (QApplication, QDialog, QGraphicsPixmapItem, QGraphicsRectItem, QGraphicsScene, QGraphicsView) from Ui_SeismicPro import Ui_SeismicProgramme class SeisPro(QtWidgets.QMainWindow, Ui_SeismicProgramme): def __init__(self, parent=None): super(SeisPro, self).__init__(parent) # 设置窗体 self.setupUi(self) # 输入框默认值设置 self.timeLineEdit.setText('0') # self.timeLineEdit.setPlainText("0") self.velocityLineEdit.setText("0") self.distanceLineEdit.setText("0") # 绘图控件,创建一个QGraphicsScene self.velocity_view.setScene(QGraphicsScene()) self.distance_view.setScene(QGraphicsScene()) # 按钮 self.oky_btn.clicked.connect(self.oky_btn_func) self.cancel_btn.clicked.connect(self.cancel_btn_fnc) def oky_btn_func(self): # 查询数据 self.distanceLineEdit.setText("1") self.add_velocitypic() self.add_distancepic() def contour_fnc(self, x, y): # 绘制等值线函数 return (1 - y**5 + x**5) * np.exp(-x**2 - y**2) def add_velocitypic(self): # 加载速度图形 frame = QImage("person.jpg") pix = QPixmap.fromImage(frame) width = self.velocity_view.width() height = self.velocity_view.height() # print(width,height) pix = pix.scaled(width, height) # 调整图片大小為 200x260 item = QGraphicsPixmapItem(pix) scene = QGraphicsScene() # QGraphicsScene 相對位置 (20, 20) 和大小 200x260 scene.setSceneRect(0, 0, width, height) #设置原点和scene大小 scene.addItem(item) self.velocity_view.setScene(scene) def add_distancepic(self): # 加载DIstance图形 frame = QImage("ttt2022831.jpg") # pix = QPixmap.fromImage(frame) # pix = pix.scaled(200, 260) # 调整图片大小為 200x260 # item = QGraphicsPixmapItem(pix) # scene = QGraphicsScene() # # QGraphicsScene 相對位置 (20, 20) 和大小 200x260 # scene.setSceneRect(0, 0, 200, 260) #设置原点和scene大小 pix = QPixmap.fromImage(frame) width = self.velocity_view.width() height = self.velocity_view.height() # print(width,height) pix = pix.scaled(width, height) # 调整图片大小為 200x260 item = QGraphicsPixmapItem(pix) scene = QGraphicsScene() # QGraphicsScene 相對位置 (20, 20) 和大小 200x260 scene.setSceneRect(0, 0, width, height) #设置原点和scene大小 scene.addItem(item) self.distance_view.setScene(scene) def resizeEvent(self, event): # 最大化窗口进行变化 super().resizeEvent(event) self.add_velocitypic() self.add_distancepic() def cancel_btn_fnc(self): # 退出按钮 print("退出程序") sys.exit() def main(): app = QApplication(sys.argv) pr = SeisPro() pr.show() app.exec() if __name__ == '__main__': main()