How can I access a class from another class in Python

67 views Asked by At

I have the two classes below. The Crop class is an example of code I found in the web for draw rectangles on images by click and drag. It's perfect for use with some modifications, for cropping parts of images. Originanlly it was a autonomous program, but now I Intended to use it together a MainClass, as a tool for capturing parts of images.

The images would be added to a QGraphicView widget generated by the MainClass, so the Crop class has to be able to access this widget. The widget, I named "Image_crop_screen.ui" was created using Qt Designer. It's a MainWindow with size 800 x 800 pixels, containing a Graphics View also 800 x 800 pixels in size.

Until now, I haven't been able to make Crop class work togther MainClass. I tried to call Crop(self). The MainWindow opens but it seems the click events is not detected.

(Sorry the bad English it's not my native language).

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import uic

CROP_UI = uic.loadUiType("Image_crop_screen.ui")[0]

class MainClass(QMainWindow, CROP_UI):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        # Graphic Screen set
        pixmap = QPixmap('')
        self.img = QGraphicsPixmapItem(pixmap)
        self.scene = QGraphicsScene()
        self.scene.addItem(self.img)
        self.graphicsView.setScene(self.scene)

        Crop(self)   # Here I tried access the class Crop


class Crop(QMainWindow):
    def __init__(self, main):
        super(Crop, self).__init__()
        self.main = main

        self.main.graphicsView.viewport().installEventFilter(self)

        self.current_item = None
        self.start_pos = QPointF()
        self.end_pos = QPointF()

    def eventFilter(self, o, e):
        if self.main.graphicsView.viewport() is o:
            if e.type() == QEvent.MouseButtonPress:
                if e.buttons() & Qt.LeftButton:
                    self.start_pos = self.end_pos = self.main.graphicsView.mapToScene(e.pos())
                    pen = QPen(QColor(255, 0, 0))
                    pen.setWidth(1)
                    brush = QBrush(QColor(0, 0, 0, 0))
                    self.current_item = self.scene.addRect(QRectF(), pen, brush)
                    self._update_item()
                    
            elif e.type() == QEvent.MouseMove:
                if e.buttons() & Qt.LeftButton and self.current_item is not None:
                    self.end_pos = self.main.graphicsView.mapToScene(e.pos())
                    self._update_item()
                    
            elif e.type() == QEvent.MouseButtonRelease:
                self.end_pos = self.main.graphicsView.mapToScene(e.pos())
                self._update_item()
                self.current_item = None

        return super().eventFilter(o, e)

    def _update_item(self):
        if self.current_item is not None:
            self.current_item.setRect(QRectF(self.start_pos, self.end_pos).normalized())
            
            
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = MainClass()
    main.show()
    app.exec_()
0

There are 0 answers