Results 1 to 2 of 2

Thread: Define the bounds of a Item in a QGraphicsView

  1. #1
    Join Date
    Sep 2015
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Define the bounds of a Item in a QGraphicsView

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Define the bounds of a Item in a QGraphicsView

    You override the itemChange() method, handle the position change and return an acceptable value if given value is not.

    Cheers,
    _

Similar Threads

  1. How to fix an Item on QgraphicsView hole.
    By Zikoel in forum Qt Programming
    Replies: 1
    Last Post: 18th September 2011, 19:49
  2. Replies: 1
    Last Post: 16th December 2009, 02:48
  3. Graphics item movement and scene bounds
    By Gianluca Magnani in forum Qt Programming
    Replies: 2
    Last Post: 24th November 2009, 09:02
  4. QGraphicsView to follow Item?
    By sven-kt in forum Qt Programming
    Replies: 5
    Last Post: 25th August 2009, 08:40
  5. MDI display Item in QGraphicsView
    By wisconxing in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2008, 00:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.