title: To draw rectangles inside label using Qpainter in python
body: <p>I am trying to draw rectangles according to a percentage value inside my labels. So it is important for me to know the exact coordinates of my labels. Since I am using multiple windows in cascade, the global positions of the current window are not retrieved accurately. Is there an easy workaround to this problem without changing my entire coding layout? form2 is my current view which is displayed as part of form1. </p>
  
  <p>It could be helpful if I can map the coordinates of my label by using mapTo some widget inside my current window. My code seems to work except for the position of my rectangle. I have tried <code>maptToGlobal()</code> and <code>mapToParent()</code> and
  <code>self.label_pos = self.mapTo(self.frame, self.label_1.geometry().bottomLeft())</code> I cannot seem to access the widgets written inside my parent class using <code>self</code>. Or is there something wrong with my syntax?</p>
  
  <pre><code>import Ui_ImageCrop # design of my window using Qt Designer
  import math
  from PyQt5.QtCore import Qt
  from PyQt5.QtWidgets import QMainWindow, QLabel
  from PyQt5.QtGui import QPainter, QPen
  
  
  class ImageCrop(Ui_ImageCrop.Ui_MainWindow, QMainWindow):
  
      def __init__(self, parent=None):
          super(ImageCrop,self ).__init__()
          self.setupUi(self)        
  
  
      def paintEvent(self,e): 
          painter  = QPainter(self)
          self.square1 = Show_Square()
  
          if self.lineEdit_1.text() == "":
              self.square1.percent = 0
          else:
              self.square1.percent = float(self.lineEdit_1.text())
  
          self.label_pos = self.mapTo(self, self.label_1.geometry().bottomLeft()) 
          self.square1.x = self.label_pos.x()             
          self.square1.y = self.label_pos.y()         
          self.square1.w = self.label_1.width()
          self.square1.h = self.label_1.height()
          self.square1.drawRectangles(painter)
          self.update()
  
  
  class Show_Square(QLabel):
  
      def __init__(self):
          super(Show_Square,self).__init__()
          self.percent = 50
          self.x = 0
          self.y = 0
          self.w = 10
          self.h = 10       
  
      def drawRectangles(self, painter):        
          center_x = float(self.x+self.w/2)
          center_y = float(self.y+self.h/2)
          rect_crop = float(self.percent*self.w*self.h/100)
          k = float(self.h/self.w)
  
          rect_w = int(math.sqrt(rect_crop/k))
          rect_h = int(k*rect_w)
          rect_x = int(center_x-rect_h/2)
          rect_y = int(center_y-rect_w/2)
  
          painter.setPen(QPen(Qt.black, 1, Qt.SolidLine))
          painter.drawRect(rect_x,rect_y,rect_w,rect_h)        
  </code></pre>