You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to use Picamera2 with PySide2 (Qt) to display a camera preview in a QML application. However, when I attempt to start the camera preview, I get the following warning and error:
`WARNING: QApplication was not created in the main() thread.
`
from PySide2.QtWidgets import QApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QObject, Slot, QUrl
import sys
import os
from picamera2 import Picamera2, Preview
class CameraHandler(QObject):
"""Handles camera operations from QML, ensuring everything runs in the main thread."""
def __init__(self, picam2):
super().__init__()
self.picam2 = picam2 # Use the instance from main()
@Slot()
def startCamera(self):
print("Starting camera...")
self.picam2.start_preview(Preview.QT) # Runs in main thread
preview_config = self.picam2.create_preview_configuration()
self.picam2.configure(preview_config)
self.picam2.start()
print("Camera started successfully!")
if __name__ == "__main__":
app = QApplication(sys.argv)
picam2 = Picamera2() # Ensure Picamera2 is created in the main thread!
engine = QQmlApplicationEngine()
camera_handler = CameraHandler(picam2) # Pass it into CameraHandler
engine.rootContext().setContextProperty("cameraHandler", camera_handler)
qml_file_path = os.path.join(os.path.dirname(__file__), "main.qml")
engine.load(QUrl.fromLocalFile(qml_file_path))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_()) # Runs the Qt event loop properly
`
Would appreciate any guidance on fixing this issue. Thanks!
The text was updated successfully, but these errors were encountered:
Picamera2 currently doesn't have PySide integration, it uses PyQt5.
I have however submitted a pull request that allows for integration as a PySide6 widget. Although this is not compatible with PySide2 I believe upgrading to PySide6 is fairly simple. The isolated preview windows you are attempting to use are however not implemented in the PySide6 integration, you would need to create and show the widgets yourself, though this doesn't take much additional work.
Description:
I am trying to use Picamera2 with PySide2 (Qt) to display a camera preview in a QML application. However, when I attempt to start the camera preview, I get the following warning and error:
`WARNING: QApplication was not created in the main() thread.
(python:75624): GLib-CRITICAL **: 14:57:13.980: g_main_context_push_thread_default: assertion 'acquired_context' failed
`
CODE
`
from PySide2.QtWidgets import QApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QObject, Slot, QUrl
import sys
import os
from picamera2 import Picamera2, Preview
`
Would appreciate any guidance on fixing this issue. Thanks!
The text was updated successfully, but these errors were encountered: