Promoting QLabel to a widget Window to show image
Unsolved
Qt for Python
-
I'm using PySide6. currently i tried to promote QLabel to it's own class so that i can setPixmap an image to it, and also draw a ROI rectangle, because without custom promotion, the rectangle is drawn in the widget background as bacause the QLabel was obviously stacked on top of the container. but when i tried to pass image signal into some function and then show it, it won't show.. is there any help that i can work on with this?
here's the code in my AnotherWidget class to show the widget window, and function to pass the image into the LabelImage() function.
class AnotherWidget(QWidget, Ui_ROIWidget): def __init__(self): super().__init__() self.setupUi(self) self.setWindowTitle("Custom ROI selection") self.imgLabel = Labelimage() @Slot() def show_image(self,image): self.imgLabel.setImage(image)
and here's the code inside the Qlabel custom subclass.
from PySide6.QtWidgets import QLabel from PySide6.QtGui import QImage, QPixmap, QPainter from PySide6.QtCore import Qt, QRect class Labelimage(QLabel): def __init__(self, parent=None): super().__init__(parent) self.image = QPixmap() self.rect = QRect() def setImage(self, image_path): try: self.image.setPixmap(QPixmap.fromImage(image_path)) self.update() except: return "error" def setRectangle(self, rect): self.rect = rect self.update() # Trigger a repaint def paintEvent(self,event): super().paintEvent(event) painter = QPainter(self) try: if not self.image.isNull(): scaled_pixmap = self.pixmap.scaled(self.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation) painter.drawPixmap(self.rect(), scaled_pixmap, scaled_pixmap.rect()) except: return "error"
the setImage() function won't show any image in the UI. it's the problem