title: Python: how to draw object on image in PyQt
body: <p><a href="https://i.stack.imgur.com/GMIXw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GMIXw.png" alt="application"></a></p>
  
  <p>i made aplication and situated objects by GridLabel. i have gray imgae in one the cells. now i want to draw rectangles only on this gray cell. but it drows only under this gray picture, and draw also out of borders of this cell. how i need to write this part of code that its lets me to draw on this gray image and to dont out from its borders?</p>
  
  <pre><code>from PyQt4 import QtGui, QtCore
  import sys, os
  
  class Example(QtGui.QWidget):
  
  def __init__(self,parent):
      super(Example, self).__init__()
      self.main_image_name="main_image.png"
      self.mode = 5
  
      self.initUI()
  
  def initUI(self):
      File_name = QtGui.QLabel('Setup file name')
      File_name_edit = QtGui.QLineEdit()
      QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
      #QMainWindow.statusBar().showMessage('Ready')
      self.setGeometry(300, 300, 250, 150)        
      self.resize(640, 360)
      self.center()
      self.main_image = QtGui.QLabel(self)
      self.main_image.setPixmap(QtGui.QPixmap(self.main_image_name))        
      btn = QtGui.QPushButton("Make setup file")    
      btn.setToolTip('Press &lt;b&gt;Detect&lt;/b&gt; button for detecting objects by your settings')
      btn.resize(btn.sizeHint())
      btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
      btn_browse = QtGui.QPushButton("Browse")
      btn_browse.clicked.connect(self.browse)
      btn_set = QtGui.QPushButton("Set name")
      #fullscreen
      #self.main_image.setScaledContents(True)
      #just centered
      self.main_image.setAlignment(QtCore.Qt.AlignCenter)
  
  
  
      #Layout
      box_File_name = QtGui.QHBoxLayout()
      box_File_name.addWidget(File_name)
      box_File_name.addWidget(File_name_edit)
      box_File_name.addWidget(btn_set)
      grid = QtGui.QGridLayout()
      grid.setSpacing(10)
      grid.addLayout(box_File_name, 1, 0)
      #grid.addWidget(File_name_edit, 1, 1)
      grid.addWidget(self.main_image, 2, 0)
      grid.addWidget(btn_browse, 3 , 0)
      grid.addWidget(btn, 4, 0)
  
      box_number = QtGui.QVBoxLayout()
      number_group=QtGui.QButtonGroup() # Number group
      r0=QtGui.QRadioButton("Traffic Lights")
      number_group.addButton(r0)
      r1=QtGui.QRadioButton("Direction")
      number_group.addButton(r1)
      r2=QtGui.QRadioButton("Traffic Lines H")
      number_group.addButton(r2)
      r3=QtGui.QRadioButton("Traffic Lines V")
      number_group.addButton(r3)
      box_number.addWidget(r0)
      box_number.addWidget(r1)
      box_number.addWidget(r2)
      box_number.addWidget(r3)
  
      r0.toggled.connect(self.radio0_clicked)
      r1.toggled.connect(self.radio1_clicked)
      r2.toggled.connect(self.radio2_clicked)
      r3.toggled.connect(self.radio3_clicked)
  
      box_road_sign = QtGui.QHBoxLayout()
      road_sign_label = QtGui.QLabel('Road signs', self)
      road_sign = QtGui.QComboBox()
      road_sign.addItem("None")
      road_sign.addItem("ex1")
      road_sign.addItem("ex2")
      road_sign.addItem("ex3")
      road_sign.addItem("ex4")
      road_sign.addItem("ex5")
      box_road_sign.addWidget(road_sign_label)
      box_road_sign.addWidget(road_sign)
      grid.addLayout(box_road_sign, 1, 1)
      grid.addLayout(box_number, 2, 1)
      self.setLayout(grid)
  
      self.show()
  def browse(self):
      w = QtGui.QWidget()
      w.resize(320, 240)
      w.setWindowTitle("Select Picture")
      filename = QtGui.QFileDialog.getOpenFileName(w, 'Open File', '/')
      self.main_image_name = filename
      self.main_image.setPixmap(QtGui.QPixmap(self.main_image_name))
  
  def center(self):
  
      qr = self.frameGeometry()
      cp = QtGui.QDesktopWidget().availableGeometry().center()
      qr.moveCenter(cp)
      self.move(qr.topLeft())
  
  
  def radio0_clicked(self, enabled):
      if enabled:
          print "0"
          self.mode=0
  
  def radio1_clicked(self, enabled):
      if enabled:
          print "1"
          self.mode=1
  
  def radio2_clicked(self, enabled):
      if enabled:
          print "2"
          self.mode=2
  
  def radio3_clicked(self, enabled):
      if enabled:
          print "3"
          self.mode=3
  
  def paintEvent( self, event) :
  
                      ????
  
  class menubarex(QtGui.QMainWindow):
      def __init__(self, parent=None):
          super(menubarex, self).__init__(parent)
          self.form_widget = Example(self) 
          self.setCentralWidget(self.form_widget)
  
      self.initUI()
  
  def initUI(self):        
      exitAction = QtGui.QAction(QtGui.QIcon('exit.png'), '&amp;Exit', self)        
      exitAction.setShortcut('Ctrl+Q')
      exitAction.setStatusTip('Exit application')
      exitAction.triggered.connect(QtGui.qApp.quit)
      menubar = self.menuBar()
      fileMenu = menubar.addMenu('&amp;File')
      fileMenu.addAction(exitAction)
  
      #self.toolbar = self.addToolBar('Exit')
      #self.toolbar.addAction(exitAction)
  
      self.statusBar().showMessage('Ready')
      self.setWindowTitle('mi ban')        
      self.setWindowIcon(QtGui.QIcon('icon.png'))
  
  def closeEvent(self, event):
  
      reply = QtGui.QMessageBox.question(self, 'Message',
          "Are you sure to quit?", QtGui.QMessageBox.Yes | 
          QtGui.QMessageBox.No)
  
      if reply == QtGui.QMessageBox.Yes:
          event.accept()
      else:
          event.ignore()
  
  def main():
  
  app = QtGui.QApplication(sys.argv)
  #ex = Example()
  menubar = menubarex()
  menubar.show()
  sys.exit(app.exec_())
  main()
  </code></pre>