Python界面设计【QT-creator基础编程 - 01】如何让不同分辨率图像自动匹配graphicsView的窗口大小

问题:

图像的分辨率比较大,是2k的。然后在这个控件下面view下面是显示不全。当然它是自动的生成了这个图上的滑动的条。它其实有滑动条,但是如果查看图像的话,看起来还是还是很方便,那么最好有一种方式就是让这个graphicsview的窗口能够自动匹配输入输出的图像的分辨率大小,让它完整显示。

环境:

使用的环境是qtcreator + python的环境。

图像处理:opencv


解决办法:

1 构建场景

python 复制代码
# 放在 Widget.__init__ 末尾即可
self.ui.graphicsView_7.setScene(QGraphicsScene())

2 更新图像算法(opencv)update_camera_frame

python 复制代码
    def update_camera_frame(self):
        if not (self.camera_manager.camera and hasattr(self.camera_manager.camera, 'read_frame')):
            return

        frame = self.camera_manager.camera.read_frame()
        if frame is None:
            return

        # BGR → RGB
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        h, w, ch = frame_rgb.shape
        q_img = QImage(frame_rgb.data, w, h, ch * w, QImage.Format_RGB888)
        pix = QPixmap.fromImage(q_img)

        # 场景已存在,直接清空 + 添加 + 自适应
        scene = self.ui.graphicsView_7.scene()
        scene.clear()
        scene.addPixmap(pix)
        self.ui.graphicsView_7.fitInView(scene.itemsBoundingRect(),
                                         Qt.KeepAspectRatio)

3 增加窗口大小变化自动缩放

python 复制代码
def resizeEvent(self, event):
    super().resizeEvent(event)
    if self.ui.graphicsView_7.scene():
        self.ui.graphicsView_7.fitInView(
            self.ui.graphicsView_7.scene().itemsBoundingRect(),
            Qt.KeepAspectRatio
        )

结果:全尺寸的显示

相关推荐
Olamyh5 小时前
【 超越 ReAct:手搓 Plan-and-Execute (Planner) Agent】
python·ai
rainbow68895 小时前
C++开源库dxflib解析DXF文件实战
开发语言·c++·开源
deepxuan5 小时前
Day7--python
开发语言·python
曲幽5 小时前
FastAPI不止于API:手把手教你用Jinja2打造动态Web页面
python·fastapi·backend·jinja2·full stack·template engine·web development
禹凕5 小时前
Python编程——进阶知识(多线程)
开发语言·爬虫·python
Ulyanov5 小时前
基于Pymunk物理引擎的2D坦克对战游戏开发
python·游戏·pygame·pymunk
铉铉这波能秀5 小时前
LeetCode Hot100数据结构背景知识之字典(Dictionary)Python2026新版
数据结构·python·算法·leetcode·字典·dictionary
蜡笔小马5 小时前
10.Boost.Geometry R-tree 空间索引详解
开发语言·c++·算法·r-tree
IOsetting5 小时前
金山云主机添加开机路由
运维·服务器·开发语言·网络·php
程序媛徐师姐5 小时前
Python基于爬虫的网络小说数据分析系统【附源码、文档说明】
爬虫·python·python爬虫·网络小说数据分析系统·pytho网络小说数据分析系统·python爬虫网络小说·python爬虫的网络小说数据