0

How can I get access to GUI resoures, for example labelroi after conforming the roi image pixmap in class Labella...if self.ui.labelroi.setPixmap(roi image) or Principal.labelroi.setPixmap(roi image) get an error, and I'm not really clear in classes instantiation mecanism... Another question: how could be the mouse release event, triggered into Labella 'heared' from Principal?

GUI

In MainWindow class:

  1. labelimage: show image

  2. labelroi: show defined roi in image

  3. pushButtonexp: explores dir

Main Program

import sys

sys.path.append("../cursoqt/sample_editor")

from PyQt5.QtWidgets import QApplication, QFileDialog, QMainWindow

from PyQt5.QtGui import QImage, QPixmap, QPainter, QPen, QGuiApplication

from PyQt5.QtCore import Qt

from paint_rectangle import * # GUI generated by pyqt designer

from labella import * # Qlabel promoted class

class Principal(QMainWindow):

    file_name = ''

    def __init__(self):
        super().__init__()      
        self.roi = QtCore.QRect()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.pushButtonexp.clicked.connect(self.openFile)            
        
        self.show()

    def openFile(self):
        file_name, _ = QFileDialog.getOpenFileName(self, 'Open file', '/home', "Images (*.png *.jpg)")
    
        if file_name != '':
            img_pixmap = Labella.convertPixmap(self, file_name)
            self.displayImg(img_pixmap)
        else:
            self.ui.labelimage.deleteRect()
    
    def displayImg(self, pixmap):
        if pixmap.isNull():             
            self.ui.labelimage.clear()
            self.ui.labelimage.deleteRect()
        else:
            self.ui.labelimage.deleteRect()
            self.ui.labelimage.setPixmap(pixmap)
            self.ui.labelimage.setCursor(Qt.CrossCursor)                
if __name__=='__main__':

    app = QApplication(sys.argv)
    window = Principal()
    window.show()
    sys.exit(app.exec_())

Qlabel promoted class

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtWidgets import QFileDialog

from paint_rectangle import * # GUI generated by pyqt designer
from call_paint_rectangle import * # Main class

class Labella(QtWidgets.QLabel):
    file_n = ''

    def __init__(self, parent):
        super().__init__(parent=parent)
  
        self.drawing_flg = False
        self.ini_coord = QtCore.QPoint()
        self.fin_coord = QtCore.QPoint()
        self.exist_img_flg = 0
        self.rect_roi = QtCore.QRect()
    
    def convertPixmap(self,fileN):
        Labella.file_n = fileN
        pixmapformat = QPixmap(Labella.file_n)
        return pixmapformat    

    def verifyPixmap(self):
        if self.pixmap():
            self.exist_img_flg = 1
        else:
            self.exist_img_flg = 0        
   
    def deleteRect(self):
        if self.rect_roi:
            self.ini_coord = self.fin_coord = QtCore.QPoint()
            self.update()

    def paintEvent(self, event):
        super().paintEvent(event)
        qp = QtGui.QPainter(self)
        qp.setPen(QtGui.QPen(QtCore.Qt.red,1,QtCore.Qt.SolidLine))
        if self.ini_coord and self.fin_coord and self.exist_img_flg == 1:
            self.rect_roi = QtCore.QRect(self.ini_coord, self.fin_coord)
            qp.drawRect(self.rect_roi)


    def mousePressEvent(self, event):
        # verifyPixmap verifies if there's a pixmap in qlabel
        self.verifyPixmap()
        if event.button() == QtCore.Qt.LeftButton and self.exist_img_flg == 1:
            self.drawing_flg = True
            self.ini_coord = self.fin_coord = event.pos() 
        elif self.exist_img_flg == 0:
            self.drawing_flg = False

    def mouseMoveEvent(self, event):
        if self.drawing_flg:
            self.fin_coord = event.pos()
            self.update()

    def mouseReleaseEvent(self, event):
        if self.drawing_flg:
            self.drawing_flg = False
            self.update()
            r_img = self.createMappedROI()
            self.ui.labelroi.setPixmap(r_img) # HERE OCCURS THE ERROR LABELLA HAVE NOT ATTRIBUTE UI
        
    def createMappedROI(self):
        pixmap_size_Rect = self.pixmap().rect()
        contents_label_Rect = QtCore.QRectF(self.contentsRect())
    
        width_rate = pixmap_size_Rect.width()/contents_label_Rect.width()
        height_rate = pixmap_size_Rect.height()/contents_label_Rect.height()
    
        mapped_roi = QtCore.QRect(int(self.rect_roi.x() * width_rate), int(self.rect_roi.y() * height_rate), 
                          int(self.rect_roi.width() * width_rate), int(self.rect_roi.height() * height_rate))

        self.qimage = QImage()
        self.qimage.load(Labella.file_n)
        crop_image = self.qimage.copy(mapped_roi)
        roi_image = QPixmap.fromImage(crop_image)
    
        return roi_image
denis
  • 1
  • 2
  • You're attempting to access an *instance*, but in your code you're actually trying to do that with the class. Please [edit] your question and show us how you tried to use signals. – musicamante Oct 03 '22 at 17:42
  • Thanks a lot for your time, I found a possible solution to pyqtsignal manage between classes in this [link](https://stackoverflow.com/questions/63975736/catch-the-mouse-event), although I don’t understand this part @QtCore.pyqtSlot()… But I would like to know how to access to widgets components from Labella class, so I could update qlabel components from inside Labella; although I think it's not a good practice to contaminate Labella class with this kind of functionality… – denis Oct 04 '22 at 14:18
  • You're correct: you should *not* do any of that from `Labella`. That's what signals are for: the object that emits the signal doesn't do anything on its own if it doesn't directly concerns itself (or its children), it's up to the common ancestor to do that. Connect the custom signal in `Principal` to the function that actually updates its contents. – musicamante Oct 04 '22 at 14:46

0 Answers0