I have a problem with pyqt6 type object 'QQuickImageProvider' has no attribute 'Image', how can I solve it?
-
I have a problem with pyqt6 type object 'QQuickImageProvider' has no attribute 'Image', how can I solve it?
-
Hi,
Which version of PySide6 are you using ?
Tested with 6.4.2 and 6.5.0 and it's working as expected.Next time, please post your code as text so it's easier to use it for testing.
-
Hi,
Which version of PySide6 are you using ?
Tested with 6.4.2 and 6.5.0 and it's working as expected.Next time, please post your code as text so it's easier to use it for testing.
import sys from PyQt6.QtCore import * from PyQt6.QtGui import * from PyQt6.QtQml import * from PyQt6.QtQuick import * class MyImageProvider(QQuickImageProvider): def __init__(self): super(MyImageProvider, self).__init__(QQuickImageProvider.Image) def requestImage(self, p_str, size): img = QImage(300, 300, QImage.Format.Format_BGR888) img.fill(Qt.green) return img, img.size() if __name__ == '__main__': app = QGuiApplication(sys.argv) engine = QQmlApplicationEngine() engine.addImageProvider("myprovider", MyImageProvider()) engine.load(QUrl.fromLocalFile("main.qml")) if len(engine.rootObjects()) == -1: sys.exit(-1) sys.exit(app.exec())
-
Hi,
Which version of PySide6 are you using ?
Tested with 6.4.2 and 6.5.0 and it's working as expected.Next time, please post your code as text so it's easier to use it for testing.
@SGaist main.qml
import QtQuick 2.15 import QtQuick.Controls 2.15 ApplicationWindow { visible: true width: 640 height: 480 title: "Display Image" Image { id: image width: parent.width height: parent.height source: image // Context property created in Python code } }
-
@SGaist main.qml
import QtQuick 2.15 import QtQuick.Controls 2.15 ApplicationWindow { visible: true width: 640 height: 480 title: "Display Image" Image { id: image width: parent.width height: parent.height source: image // Context property created in Python code } }
You forgot to mention which version of
PySidePyQt 6 you are using.Also, how did you install it ?
-
You forgot to mention which version of
PySidePyQt 6 you are using.Also, how did you install it ?
-
Sorry, I misread the imports.
Anyway, the solution to your issue:
class MyImageProvider(QQuickImageProvider): def __init__(self): super(MyImageProvider, self).__init__(QQuickImageProvider.ImageType.Image) def requestImage(self, p_str, size): img = QImage(300, 300, QImage.Format.Format_BGR888) img.fill(Qt.green) return img, img.size()
-
Sorry, I misread the imports.
Anyway, the solution to your issue:
class MyImageProvider(QQuickImageProvider): def __init__(self): super(MyImageProvider, self).__init__(QQuickImageProvider.ImageType.Image) def requestImage(self, p_str, size): img = QImage(300, 300, QImage.Format.Format_BGR888) img.fill(Qt.green) return img, img.size()
-
Sorry, I misread the imports.
Anyway, the solution to your issue:
class MyImageProvider(QQuickImageProvider): def __init__(self): super(MyImageProvider, self).__init__(QQuickImageProvider.ImageType.Image) def requestImage(self, p_str, size): img = QImage(300, 300, QImage.Format.Format_BGR888) img.fill(Qt.green) return img, img.size()
@SGaist
now i got code like this i want cv2 to show in ui qml page more solution please help meimport sys from pathlib import Path import os import cv2 from PyQt6.QtGui import QGuiApplication from PyQt6.QtQml import QQmlApplicationEngine from PyQt6.QtGui import QIcon, QPixmap, QImage from PyQt6.QtWidgets import QFileDialog, QApplication from PyQt6.QtCore import Qt, QThread, pyqtSignal, pyqtSlot, QObject, QSize from PyQt6.QtQuick import QQuickPaintedItem, QQuickView, QQuickImageProvider class ThreadCamera(QThread): updateFrame = pyqtSignal(QImage) def __init__(self, parent=None): QThread.__init__(self, parent) def run(self): self.cap = cv2.VideoCapture(0) while self.cap.isOpened(): ret, frame = self.cap.read() if not ret: img = QImage("./images/network.png") self.updateFrame.emit(img) color_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) img = QImage(color_frame.data, color_frame.shape[1], color_frame.shape[0], QImage.Format.Format_RGB888) self.updateFrame.emit(img) class ImageProvider(QQuickImageProvider): imageChanged = pyqtSignal(QImage) def __init__(self): super(ImageProvider, self).__init__(QQuickImageProvider.ImageType.Image) self.cam = ThreadCamera() self.cam.updateFrame.connect(self.update_image) self.image = None def requestImage(self, id, size): if self.image: img = self.image else: img = QImage(600, 500, QImage.Format.Format_RGBA8888) img.fill(Qt.GlobalColor.black) return img,img.size() @pyqtSlot() def update_image(self, img): self.imageChanged.emit(img) self.image = img @pyqtSlot() def start(self): print("Starting...") self.cam.start() @pyqtSlot() def killThread(self): print("Finishing...") try: self.cam.cap.release() cv2.destroyAllWindows() except: pass class MainWindow(QObject): def __init__(self): QObject.__init__(self) if __name__ == "__main__": app = QApplication(sys.argv) app.setWindowIcon(QIcon("./images/network.png")) engine = QQmlApplicationEngine() #Get Context main = MainWindow() myImageProvider = ImageProvider() engine.rootContext().setContextProperty("backend", main) engine.rootContext().setContextProperty("myImageProvider", myImageProvider) engine.addImageProvider("MyImageProvider", myImageProvider) #Load QML File engine.load(os.fspath(Path(__file__).resolve().parent / "main.qml")) if not engine.rootObjects(): sys.exit(-1) sys.exit(app.exec())
main.qml
import QtQuick 6.3 import QtQuick.Window 6.3 import QtMultimedia 6.3 import QtQuick.Controls 6.3 import QtQuick.Layouts 6.3 import QtQuick.Dialogs 6.3 Window { visible: true width: 600 height: 500 title: "WebCam" Image { id: feedImage width: parent.width height: parent.height - 50 fillMode: Image.PreserveAspectFit cache: false source: "image://MyImageProvider/img" property bool counter: false function reloadImage() { counter = !counter source = "image://MyImageProvider/img?id=" + counter } } RowLayout { anchors.top: feedImage.bottom anchors.horizontalCenter: feedImage.horizontalCenter Button { id: btnStartCamera text: "Start Camera" onClicked: { myImageProvider.start() } } Button { id: btnStopCamera text: "Stop Camera" onClicked: { myImageProvider.killThread() } } } Connections{ target: myImageProvider function onImageChanged(image) { console.log("emit") feedImage.reloadImage() } } }
-
@SGaist
Now there is an issue with TypeError: update_image() missing 1 required positional argument: 'img'.
which if I use PySide6 it works but I want to use it with PyQt6 because it has to be merged with the created project.You did not specify the type of the argument if your slot in pyqtSlot.