Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to resize a item QGraphicsView?

How to resize a item QGraphicsView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 5.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pedro_Monteiro
    wrote on last edited by Pedro_Monteiro
    #1

    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 I can pass the image boundaries and I wanted to limit the rectangle to the image boundary, how could I do that? And another question, how could I change the size of the rectangle using the mouse?

    Down a code I'm using for testing, it gets QGraphicsView I did in Designer:

    # -*- coding: utf-8 -*-
    
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    import sys
    from screen import *
    
    class Main(QWidget, Ui_Form):
    	def __init__(self, parent=None):
    		super(Main, self).__init__(parent)
    		self.setupUi(self)
    
    		self.scene = QGraphicsScene()
    		self.cena.setScene(self.scene)
    		self.scene.addPixmap(QPixmap("01.png"))
    
    		pen = QPen(Qt.darkMagenta)
    		pen.setWidth(4)
    
    		rectangle = self.scence.addRect(5,5,300,150, pen)
    		rectangle.setFlag(QGraphicsItem.ItemIsMovable)
    
    	def showEvent(self, event):
    		self.cena.fitInView(self.scene.sceneRect(), Qt.IgnoreAspectRatio)
    
    app = QApplication(sys.argv)
    window = Main()
    window.show()
    
    sys.exit(app.exec_())
    

    The answer can be in C++ or Python.
    Thanks.

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      i will give you an (rough) overview what has to be done:

      MOVING

      1. subclass QGraphicsRectItem (add the rect-item as a child of the pixmap item)
      2. on itself call setFlags(QGraphicsItem::ItemSendsGeometryChanges)
      3. override itemChange() and listen to QGraphicsItem::ItemPositionChange and check the value if it is in the parent-item's rect/shape. If not return the adapted value

      RESIZING

      1. override the paint event handler of the created custom rect item and paint 4 small rects in each corner of the rect
      2. overrride mousePressEvent() handler and check if the mouse was clicked on a handle, if so save the handle direction in a local variable
      3. overrride mouseMoveEvent() handler and check the variable for the direction, calculate the diff-distance between the last position and the current move-position. Add the diff to the current rect of the item. Also make sure the (like you also have done it for MOVING) that the rect stays inside the parent-item's rect
      4. overrride hoverMoveEvent() handler in case you want to change the cursor when the cursor is over one of the handles

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      P 1 Reply Last reply
      0
      • raven-worxR raven-worx

        i will give you an (rough) overview what has to be done:

        MOVING

        1. subclass QGraphicsRectItem (add the rect-item as a child of the pixmap item)
        2. on itself call setFlags(QGraphicsItem::ItemSendsGeometryChanges)
        3. override itemChange() and listen to QGraphicsItem::ItemPositionChange and check the value if it is in the parent-item's rect/shape. If not return the adapted value

        RESIZING

        1. override the paint event handler of the created custom rect item and paint 4 small rects in each corner of the rect
        2. overrride mousePressEvent() handler and check if the mouse was clicked on a handle, if so save the handle direction in a local variable
        3. overrride mouseMoveEvent() handler and check the variable for the direction, calculate the diff-distance between the last position and the current move-position. Add the diff to the current rect of the item. Also make sure the (like you also have done it for MOVING) that the rect stays inside the parent-item's rect
        4. overrride hoverMoveEvent() handler in case you want to change the cursor when the cursor is over one of the handles
        P Offline
        P Offline
        Pedro_Monteiro
        wrote on last edited by
        #3

        @raven-worx I already solved about moving, but the resizing looks hard to do, is there some example, even in C++?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved