I have designed a GUI using QtDesigner which is a MainWindow with central widget that only has a graphics view.

In the following Python code, I used openCV to get a camera connection and tried to show camera feed inside the graphics view:
from Ui.uiLoader import load_ui
from PySide6.QtWidgets import QWidget, QGraphicsScene, QGraphicsPixmapItem
from PySide6.QtCore import QTimer, Qt
from PySide6.QtGui import QImage, QPixmap, QPen
import cv2
class MainWindowTest:
def __init__(self):
# Load main window
self.ui = load_ui("graphicsviewTest.ui")
self.graphicsview = self.ui.graphicsView
self.scene = QGraphicsScene()
self.pixmap_item = QGraphicsPixmapItem()
self.scene.addItem(self.pixmap_item)
self.graphicsview.setScene(self.scene)
# Persistent camera object
self.cap = cv2.VideoCapture(0)
# Timer drives the video stream
self.timer = QTimer()
self.timer.timeout.connect(self.update_frame)
self.timer.start(30) # ~33 FPS
def update_frame(self):
ret, frame = self.cap.read()
if not ret:
return
qt_image = self.convert_cv_qt(frame)
pixmap = QPixmap.fromImage(qt_image)
self.pixmap_item.setPixmap(pixmap)
self.scene.setSceneRect(pixmap.rect())
self.graphicsview.fitInView(self.scene.sceneRect(),Qt.KeepAspectRatio)
def convert_cv_qt(self, frame):
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = rgb.shape
bytes_per_line = ch * w
image = QImage(
rgb.data,
w,
h,
bytes_per_line,
QImage.Format_RGB888
)
return image.copy()
This code is running BUT what is shown inside the graphicsview is a cropped camera stream (Not the whole scene that the camera is already seeing).
How can I got this problem (cropped camera scene) solved?