1

PyQt5系列教程(三十五)QGraphicsView显示图片

 1 year ago
source link: https://xugaoxiang.com/2022/12/01/pyqt5-35-qgraphicsview/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

软硬件环境

  • Windows 10 64bit
  • Anaconda3 with python 3.8
  • PyQt5 5.15

QGraphicsViewPyQt5 里负责显示图片的控件,它需要搭配 QGraphicsSceneQtGui.QPixmap 就可以显示图片了,本篇我们就来看看如何实现。

打开 designer.exe,新建一个窗口,然后拖入 QGraphics View

a268067e5113709f.png

保存成 ui 文件后,使用下面命令进行代码转换

pyuic5.bat -o ui.py project.ui

接着看看主程序 main.py

import sys

from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsScene
from PyQt5.QtGui import QPixmap
from ui import Ui_MainWindow

class MainWindow(QMainWindow, Ui_MainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        scene = QGraphicsScene()
        img = QPixmap('Lenna.jpg')
        # 图片拉伸到窗口大小
        img = img.scaled(self.window().width(), self.window().height())
        scene.addPixmap(img)
        # 绑定scene
        self.graphicsView.setScene(scene)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    windows = MainWindow()
    windows.show()
    sys.exit(app.exec_())

最后执行结果如下

eb6d0df2c0de5d6b.png

https://github.com/xugaoxiang/learningPyQt5

PyQt5系列教程

更多PyQt5教程,请移步

https://xugaoxiang.com/category/python/pyqt5/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK