问题:
图像的分辨率比较大,是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
)
结果:全尺寸的显示