Hello, I made an application using the Qt Designer and PyQt4, I'm using QGraphicsView to display an image with a rectangle on it, I can move the rectangle over the image, but when I move it with the mouse I can pass the image boundaries and I wanted to limit the rectangle to the image boundary, how could I do that?
The answer can be in C++.
My image with rectangle
Thanks.

Qt Code:
  1. # -*- coding: utf-8 -*-
  2.  
  3. from PyQt4.QtCore import *
  4. from PyQt4.QtGui import *
  5. import sys
  6. from screen import *
  7.  
  8. class Main(QWidget, Ui_Form):
  9. def __init__(self, parent=None):
  10. super(Main, self).__init__(parent)
  11. self.setupUi(self)
  12.  
  13. self.scene = QGraphicsScene()
  14. self.cena.setScene(self.scene)
  15. self.scene.addPixmap(QPixmap("01.png"))
  16.  
  17. pen = QPen(Qt.darkMagenta)
  18. pen.setWidth(4)
  19.  
  20. rectangle = self.scence.addRect(5,5,300,150, pen)
  21. rectangle.setFlag(QGraphicsItem.ItemIsMovable)
  22.  
  23. def showEvent(self, event):
  24. self.cena.fitInView(self.scene.sceneRect(), Qt.IgnoreAspectRatio)
  25.  
  26. app = QApplication(sys.argv)
  27. window = Main()
  28. window.show()
To copy to clipboard, switch view to plain text mode