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:
labelimage: show image
labelroi: show defined roi in image
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