Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOW-TO] Picamera2 with PySide2 (Qt) - QApplication Not Created in Main() Thread #1197

Open
Akshar062 opened this issue Jan 24, 2025 · 1 comment

Comments

@Akshar062
Copy link

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

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!

@LODean
Copy link

LODean commented Jan 24, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants