Related
I'm working on a GUI in Python with PySide2. I have a GraphicsView, where I'll put an image, and I'd like to draw and move a polygon around on that image. I've found many examples of simply drawing polygons, circles, etc. in PySide, PySide2, or PyQt 4/5 in Python. However, I haven't been able to figure out why my graphics items do not move on an event without deleting and redrawing.
I'm using the keyboard to change the X value on a PySide2 QRectF. The X value is clearly changing, but the rectangle does not actually move.
Here is a minimal example:
from PySide2 import QtCore, QtGui, QtWidgets
from functools import partial
class DebuggingDrawing(QtWidgets.QGraphicsView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# initialize the scene and set the size
self._scene = QtWidgets.QGraphicsScene(self)
self._scene.setSceneRect(0,0,500,500)
self.setScene(self._scene)
# make a green pen and draw a 10 wide, 20 high rectangle at x=20, y=30
self.pen = QtGui.QPen(QtCore.Qt.green, 0)
self.draw_rect = QtCore.QRectF(20, 30, 10, 20)
# add the rectangle to our scene
self._scene.addRect(self.draw_rect, self.pen)
def move_rect(self, dx: int):
# method for moving the existing rectangle
# get the x value
x = self.draw_rect.x()
print('x: {} dx: {}'.format(x, dx))
# use the moveLeft method of QRectF to change the rectangle's left side x value
self.draw_rect.moveLeft(x + dx)
self.update()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.labelImg = DebuggingDrawing()
# Get a keyboard shortcut and hook it up to the move_rect method
next_shortcut = QtWidgets.QShortcut(QtGui.QKeySequence('Right'), self)
next_shortcut.activated.connect(partial(self.labelImg.move_rect, 1))
# get the left key shortcut, move_rect one pixel left
back_shortcut = QtWidgets.QShortcut(QtGui.QKeySequence('Left'), self)
back_shortcut.activated.connect(partial(self.labelImg.move_rect, -1))
self.setCentralWidget(self.labelImg)
self.setMaximumHeight(480)
self.update()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
testing = MainWindow()
testing.show()
app.exec_()
Here's what the output looks like:
You clearly can't see in the image, but even though the rectangle's x value is changing according to our print calls, nothing moves around in the image. I've confirmed it's not just my eyes, because if I draw new rectangles in move_rect, they clearly show up.
draw_rect is a QRectF is an input to create an item(QGraphicsRectItem) that is returned by the addRect() method similar to pen, that is, it takes the information but then no longer uses it. The idea is to move the item using setPos():
class DebuggingDrawing(QtWidgets.QGraphicsView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# initialize the scene and set the size
self._scene = QtWidgets.QGraphicsScene(self)
self._scene.setSceneRect(0, 0, 500, 500)
self.setScene(self._scene)
# make a green pen and draw a 10 wide, 20 high rectangle at x=20, y=30
pen = QtGui.QPen(QtCore.Qt.green, 0)
draw_rect = QtCore.QRectF(20, 30, 10, 20)
# add the rectangle to our scene
self.item_rect = self._scene.addRect(draw_rect, pen)
def move_rect(self, dx: int):
p = self.item_rect.pos()
p += QtCore.QPointF(dx, 0)
self.item_rect.setPos(p)
If you still want to use draw_rect then you have to set it again in the item:
self.pen = QtGui.QPen(QtCore.Qt.green, 0)
self.draw_rect = QtCore.QRectF(20, 30, 10, 20)
# add the rectangle to our scene
self.item_rect = self._scene.addRect(self.draw_rect, self.pen)
def move_rect(self, dx: int):
# method for moving the existing rectangle
# get the x value
x = self.draw_rect.x()
print('x: {} dx: {}'.format(x, dx))
# use the moveLeft method of QRectF to change the rectangle's left side x value
self.draw_rect.moveLeft(x + dx)
self.item_rect.setRect(self.draw_rect)
It is recommended that "Graphics View Framework" be read so that the QGraphicsItems, QGraphicsView and QGraphicsScene work.
I have user-adjustable annotations in a graphics scene. The size/rotation of annotations is handled by dragging corners of a rectangle about the annotation. I'm using a custom rect (instead of the boundingRect) so it follows the rotation of the parent annotation. The control corners are marked by two ellipses whose parent is the rect so transformations of rect/ellipse/annotation are seamless.
I want to detect when the cursor is over one of the corners, which corner it is, and the exact coordinates. For this task it seems that I should filter the hoverevents with the parent rect using a sceneEventFilter.
I've tried umpty zilch ways of implementing the sceneEventFilter to no avail. All events go directly to the hoverEnterEvent function. I've only found a few bits of example code that do something like this but I'm just plain stuck. btw, I'm totally self taught on Python and QT over the past 3 months, so please bear with me. I'm sure I'm missing something very basic. The code is a simplified gui with two ellipses. We're looking to capture events in the sceneEventFilter but always goes to hoverEnterEvent.
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsItem
import sys
class myHandle(QtGui.QGraphicsEllipseItem):
def __init__(self, parent = None):
super(myHandle, self).__init__(parent)
def addTheHandle(self, h_parent = 'null', kind = 'null'):
handle_w = 40
if kind == 'scaling handle':
handle_x = h_parent.boundingRect().topRight().x() - handle_w/2
handle_y = h_parent.boundingRect().topRight().y() - handle_w/2
if kind == 'rotation handle':
handle_x = h_parent.boundingRect().topLeft().x() - handle_w/2
handle_y = h_parent.boundingRect().topLeft().y() - handle_w/2
the_handle = QtGui.QGraphicsEllipseItem(QtCore.QRectF(handle_x, handle_y, handle_w, handle_w))
the_handle.setPen(QtGui.QPen(QtGui.QColor(255, 100, 0), 3))
the_handle.setParentItem(h_parent)
the_handle.setAcceptHoverEvents(True)
the_handle.kind = kind
return the_handle
class myRect(QtGui.QGraphicsRectItem):
def __init__(self, parent = None):
super(myRect, self).__init__(parent)
def rectThing(self, boundingrectangle):
self.setAcceptHoverEvents(True)
self.setRect(boundingrectangle)
mh = myHandle()
rotation_handle = mh.addTheHandle(h_parent = self, kind = 'rotation handle')
scaling_handle = mh.addTheHandle(h_parent = self, kind = 'scaling handle')
self.installSceneEventFilter(rotation_handle)
self.installSceneEventFilter(scaling_handle)
return self, rotation_handle, scaling_handle
def sceneEventFilter(self, event):
print('scene ev filter')
return False
def hoverEnterEvent(self, event):
print('hover enter event')
class Basic(QtGui.QMainWindow):
def __init__(self):
super(Basic, self).__init__()
self.initUI()
def eventFilter(self, source, event):
return QtGui.QMainWindow.eventFilter(self, source, event)
def exit_the_program(self):
pg.exit()
def initUI(self):
self.resize(300, 300)
self.centralwidget = QtGui.QWidget()
self.setCentralWidget(self.centralwidget)
self.h_layout = QtGui.QHBoxLayout(self.centralwidget)
self.exit_program = QtGui.QPushButton('Exit')
self.exit_program.clicked.connect(self.exit_the_program)
self.h_layout.addWidget(self.exit_program)
self.this_scene = QGraphicsScene()
self.this_view = QGraphicsView(self.this_scene)
self.this_view.setMouseTracking(True)
self.this_view.viewport().installEventFilter(self)
self.h_layout.addWidget(self.this_view)
self.circle = self.this_scene.addEllipse(QtCore.QRectF(40, 40, 65, 65), QtGui.QPen(QtCore.Qt.black))
mr = myRect()
the_rect, rotation_handle, scaling_handle = mr.rectThing(self.circle.boundingRect())
the_rect.setPen(QtGui.QPen(QtCore.Qt.black))
the_rect.setParentItem(self.circle)
self.this_scene.addItem(the_rect)
self.this_scene.addItem(rotation_handle)
self.this_scene.addItem(scaling_handle)
def main():
app = QtGui.QApplication([])
main = Basic()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
The main problem is that you are installing the event filter of the target items on the rectangle: the event filter of the rectangle will never receive anything. Moreover, sceneEventFilter accepts two arguments (the watched item and the event), but you only used one.
What you should do is to install the event filter of the rectangle on the target items:
rotation_handle.installSceneEventFilter(self)
scaling_handle.installSceneEventFilter(self)
That said, if you want to use those ellipse items for scaling or rotation of the source circle, your approach is a bit wrong to begin with.
from math import sqrt
# ...
class myRect(QtGui.QGraphicsRectItem):
def __init__(self, parent):
super(myRect, self).__init__(parent)
self.setRect(parent.boundingRect())
# rotation is usually based on the center of an object
self.parentItem().setTransformOriginPoint(self.parentItem().rect().center())
# a rectangle that has a center at (0, 0)
handleRect = QtCore.QRectF(-20, -20, 40, 40)
self.rotation_handle = QtGui.QGraphicsEllipseItem(handleRect, self)
self.scaling_handle = QtGui.QGraphicsEllipseItem(handleRect, self)
# position the handles by centering them at the right corners
self.rotation_handle.setPos(self.rect().topLeft())
self.scaling_handle.setPos(self.rect().topRight())
for source in (self.rotation_handle, self.scaling_handle):
# install the *self* event filter on the handles
source.installSceneEventFilter(self)
source.setPen(QtGui.QPen(QtGui.QColor(255, 100, 0), 3))
def sceneEventFilter(self, source, event):
if event.type() == QtCore.QEvent.GraphicsSceneMouseMove:
# map the handle event position to the ellipse parent item; we could
# also map to "self", but using the parent is more consistent
localPos = self.parentItem().mapFromItem(source, event.pos())
if source == self.rotation_handle:
# create a temporary line to get the rotation angle
line = QtCore.QLineF(self.boundingRect().center(), localPos)
# add the current rotation to the angle between the center and the
# top left corner, then subtract the new line angle
self.parentItem().setRotation(135 + self.parentItem().rotation() - line.angle())
# note that I'm assuming that the ellipse is a circle, so the top
# left angle will always be at 135°; if it's not a circle, the
# rect width and height won't match and the angle will be
# different, so you'll need to compute that
# parentRect = self.parentItem().rect()
# oldLine = QtCore.QLineF(parentRect.center(), parentRect.topLeft())
# self.parentItem().setRotation(
# oldLine.angle() + self.parentItem().rotation() - line.angle())
elif source == self.scaling_handle:
# still assuming a perfect circle, so the rectangle is a square;
# the line from the center to the top right corner is used to
# compute the square side size, which is the double of a
# right-triangle cathetus where the hypotenuse is the line
# between the center and any of its corners;
# if the ellipse is not a perfect circle, you'll have to
# compute both of the catheti
hyp = QtCore.QLineF(self.boundingRect().center(), localPos)
size = sqrt(2) * hyp.length()
rect = QtCore.QRectF(0, 0, size, size)
rect.moveCenter(self.rect().center())
self.parentItem().setRect(rect)
self.setRect(rect)
# update the positions of both handles
self.rotation_handle.setPos(self.rect().topLeft())
self.scaling_handle.setPos(self.rect().topRight())
return True
elif event.type() == QtCore.QEvent.GraphicsSceneMousePress:
# return True to the press event (which is almost as setting it as
# accepted, so that it won't be processed further more by the scene,
# allowing the sceneEventFilter to capture the following mouseMove
# events that the watched graphics items will receive
return True
return super(myRect, self).sceneEventFilter(source, event)
class Basic(QtGui.QMainWindow):
# ...
def initUI(self):
# ...
self.circle = self.this_scene.addEllipse(QtCore.QRectF(40, 40, 65, 65), QtGui.QPen(QtCore.Qt.black))
mr = myRect(self.circle)
self.this_scene.addItem(mr)
I'm learning python and PySide2 and following up some tutorials from learnpytq, specifically https://www.learnpyqt.com/courses/custom-widgets/bitmap-graphics/ and I'm stuck at a point.
Down the line, after creating the pixmap canvas, we move the mouseMoveEvent on the widget in order to ensure that the coordinates of the mouse are always relative to the canvas. I've copied the source provided but still in my running app, the mouse position is relative to the window (or parent widget, I'm not sure), resulting in a line drawn offset to the mouse position.
Here's the code:
import sys
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCore import Qt
class Canvas(QtWidgets.QLabel):
def __init__(self):
super().__init__()
pixmap = QtGui.QPixmap(600, 300)
self.setPixmap(pixmap)
self.last_x, self.last_y = None, None
self.pen_color = QtGui.QColor('#000000')
def set_pen_color(self, c):
self.pen_color = QtGui.QColor(c)
def mouseMoveEvent(self, e):
if self.last_x is None: # First event.
self.last_x = e.x()
self.last_y = e.y()
return # Ignore the first time.
painter = QtGui.QPainter(self.pixmap())
p = painter.pen()
p.setWidth(4)
p.setColor(self.pen_color)
painter.setPen(p)
painter.drawLine(self.last_x, self.last_y, e.x(), e.y())
painter.end()
self.update()
# Update the origin for next time.
self.last_x = e.x()
self.last_y = e.y()
def mouseReleaseEvent(self, e):
self.last_x = None
self.last_y = None
COLORS = [
# 17 undertones https://lospec.com/palette-list/17undertones
'#000000', '#141923', '#414168', '#3a7fa7', '#35e3e3', '#8fd970', '#5ebb49',
'#458352', '#dcd37b', '#fffee5', '#ffd035', '#cc9245', '#a15c3e', '#a42f3b',
'#f45b7a', '#c24998', '#81588d', '#bcb0c2', '#ffffff',
]
class QPaletteButton(QtWidgets.QPushButton):
def __init__(self, color):
super().__init__()
self.setFixedSize(QtCore.QSize(24,24))
self.color = color
self.setStyleSheet("background-color: %s;" % color)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.canvas = Canvas()
w = QtWidgets.QWidget()
l = QtWidgets.QVBoxLayout()
w.setLayout(l)
l.addWidget(self.canvas)
palette = QtWidgets.QHBoxLayout()
self.add_palette_buttons(palette)
l.addLayout(palette)
self.setCentralWidget(w)
def add_palette_buttons(self, layout):
for c in COLORS:
b = QPaletteButton(c)
b.pressed.connect(lambda c=c: self.canvas.set_pen_color(c))
layout.addWidget(b)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
Can anyone spot what I'm doing wrong?
The problem comes from the fact that you're drawing according to the widget coordinates, and not those of the actual "canvas" (the "embedded" pixmap), which can be translated if the space available to the QLabel is bigger than the QPixmap size.
If, for example, the image is vertically centered, you resize the window and the label height becomes 400 (which is bigger than the pixmap height), whenever you click at position 100, 100, that position will be actually vertically translated by 50 pixel (the height of the label minus the height of the image, divided by 2).
To actually get the position according to the pixmap you have to compute it by yourself, and then translate the mouse point accordingly:
def mouseMoveEvent(self, e):
if self.last_x is None: # First event.
self.last_x = e.x()
self.last_y = e.y()
return # Ignore the first time.
rect = self.contentsRect()
pmRect = self.pixmap().rect()
if rect != pmRect:
# the pixmap rect is different from that available to the label
align = self.alignment()
if align & QtCore.Qt.AlignHCenter:
# horizontally align the rectangle
pmRect.moveLeft((rect.width() - pmRect.width()) / 2)
elif align & QtCore.Qt.AlignRight:
# align to bottom
pmRect.moveRight(rect.right())
if align & QtCore.Qt.AlignVCenter:
# vertically align the rectangle
pmRect.moveTop((rect.height() - pmRect.height()) / 2)
elif align & QtCore.Qt.AlignBottom:
# align right
pmRect.moveBottom(rect.bottom())
painter = QtGui.QPainter(self.pixmap())
p = painter.pen()
p.setWidth(4)
p.setColor(self.pen_color)
painter.setPen(p)
# translate the painter by the pmRect offset; note the negative sign
painter.translate(-pmRect.topLeft())
painter.drawLine(self.last_x, self.last_y, e.x(), e.y())
painter.end()
self.update()
# Update the origin for next time.
self.last_x = e.x()
self.last_y = e.y()
In my program I'm trying to map the coordinates of a mousepress back to the coordinate dimensions of an image. I'm using PyQt4 in Python. The program below demonstrates the problem. I have a widget that makes a few image transformations. After those image transformations the image is shown in the center of the widget, while maintaining the original aspect ratio of the image. Since the image is scaled and translated, a coordinate of a MouseEvent must be remapped to the coordinate system of the Image.
The program below has a class "ScalingWidget", that should be able to do these transformations and should also be able to remap the coordinate in a mouseReleaseEvent back to the coordinate system of the image. This works perfectly as expected when I show the widget outside a Layout and mainwindow, but it gets evil when I embed the widget in a bigger gui. Then the coordinates after mapping them back to the Image coordinates suddenly displays an offset.
The minimal program below can be started with and without the bug by specifing flag -b when starting the program. The option -n can put the instance of ScalingWidget deep and deeper inside a "gui", and the deeper it is embedded in the layouts the more strong the bug will be visible.
The stupid thing is, although drawing indicates that the transformations are correct, the mapped coordinates (printed in the window title and console) indicate that remapping them back to the image coordinates is screwed up when the -b flag is present.
So my question is: What am I doing wrong with remapping the mouse coordinates back to the image dimensions when my ScalingWidget is embedded in a layout?
I don't expect the remapping to be pixel perfect, but just as accurate as the end user can position the mouse. There are two points x=20, y=20 and at x=380 and y=380 these can be used as reference point.
Any help is most welcome!
#!/usr/bin/env python
from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
import argparse
class ScalingWidget (QtGui.QWidget):
''' Displays a pixmap optimally in the center of the widget, in such way
the pixmap is shown in the middle
'''
white = QtGui.QColor(255,255,255)
black = QtGui.QColor( 0, 0, 0)
arcrect = QtCore.QRect(-10, -10, 20, 20)
def __init__(self):
super(ScalingWidget, self).__init__()
self.pixmap = QtGui.QPixmap(400, 400)
painter = QtGui.QPainter(self.pixmap)
painter.fillRect(self.pixmap.rect(), self.white)
self.point1 = QtCore.QPoint(20, 20)
self.point2 = QtCore.QPoint(380, 380)
painter.setPen(self.black)
painter.drawRect(QtCore.QRect(self.point1, self.point2))
painter.end()
self.matrix = None
def sizeHint(self):
return QtCore.QSize(500,400)
##
# Applies the default transformations
#
def _default_img_transform(self, painter):
#size of widget
winheight = float(self.height())
winwidth = float(self.width())
#size of pixmap
scrwidth = float(self.pixmap.width())
scrheight = float(self.pixmap.height())
assert(painter.transform().isIdentity())
if scrheight <= 0 or scrwidth <= 0:
raise RuntimeError(repr(self) + "Unable to determine Screensize")
widthr = winwidth / scrwidth
heightr = winheight / scrheight
if widthr > heightr:
translate = (winwidth - heightr * scrwidth) /2
painter.translate(translate, 0)
painter.scale(heightr, heightr)
else:
translate = (winheight - widthr * scrheight) / 2
painter.translate(0, translate)
painter.scale(widthr, widthr)
# now store the matrix used to map the mouse coordinates back to the
# coordinates of the pixmap
self.matrix = painter.deviceTransform()
def paintEvent(self, e):
painter = QtGui.QPainter(self)
painter.setClipRegion(e.region())
# fill the background of the entire widget.
painter.fillRect(self.rect(), QtGui.QColor(0,0,0))
# transform to place the image nicely in the center of the widget.
self._default_img_transform(painter)
painter.drawPixmap(self.pixmap.rect(), self.pixmap, self.pixmap.rect())
pen = QtGui.QPen(QtGui.QColor(255,0,0))
# Just draw on the points used to make the black rectangle of the pix map
# drawing is not affected, be remapping those coordinates with the "same"
# matrix is.
pen.setWidth(4)
painter.setPen(pen)
painter.save()
painter.translate(self.point1)
painter.drawPoint(0,0)
painter.restore()
painter.save()
painter.translate(self.point2)
painter.drawPoint(0,0)
painter.restore()
painter.end()
def mouseReleaseEvent(self, event):
x, y = float(event.x()), float(event.y())
inverted, invsucces = self.matrix.inverted()
assert(invsucces)
xmapped, ymapped = inverted.map(x,y)
print x, y
print xmapped, ymapped
self.setWindowTitle("mouse x,y = {}, {}, mapped x, y = {},{} "
.format(x, y, xmapped, ymapped)
)
def start_bug():
''' Displays the mouse press mapping bug.
This is a bit contrived, but in the real world
a widget is embedded in deeper in a gui
than a single widget, besides the problem
grows with the depth of embedding.
'''
app = QtGui.QApplication(sys.argv)
win = QtGui.QWidget()
layout = QtGui.QVBoxLayout()
win.setLayout(layout)
widget = None
for i in range(0, args.increase_bug):
if i < args.increase_bug-1:
widget = QtGui.QWidget()
layout.addWidget(widget)
layout= QtGui.QVBoxLayout()
widget.setLayout(layout)
else:
layout.addWidget(ScalingWidget())
win.show()
sys.exit(app.exec_())
def start_no_bug():
''' Does not show the mapping bug, the mouse event.x() and .y() map nicely back to
the coordinate system of the pixmap
'''
app = QtGui.QApplication(sys.argv)
win = ScalingWidget()
win.show()
sys.exit(app.exec_())
# parsing arguments
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-b', '--display-bug', action='store_true',
help="Toggle this option to get the bugged version"
)
parser.add_argument('-n', '--increase-bug', type=int, default=1,
help="Increase the bug by n times."
)
if __name__ == "__main__":
args = parser.parse_args()
if args.display_bug:
start_bug()
else:
start_no_bug()
The basic idea of the _default_image_transform is correct. The error is in the end of the function.
def _default_img_transform(self, painter):
#size of widget
winheight = float(self.height())
winwidth = float(self.width())
#size of pixmap
scrwidth = float(self.pixmap.width())
scrheight = float(self.pixmap.height())
assert(painter.transform().isIdentity())
if scrheight <= 0 or scrwidth <= 0:
raise RuntimeError(repr(self) + "Unable to determine Screensize")
widthr = winwidth / scrwidth
heightr = winheight / scrheight
if widthr > heightr:
translate = (winwidth - heightr * scrwidth) /2
painter.translate(translate, 0)
painter.scale(heightr, heightr)
else:
translate = (winheight - widthr * scrheight) / 2
painter.translate(0, translate)
painter.scale(widthr, widthr)
# now store the matrix used to map the mouse coordinates back to the
# coordinates of the pixmap
self.matrix = painter.deviceTransform() ## <-- error is here
The last line of the function _default_image_transform should be:
self.matrix = painter.transform()
According to the documentation one should only call the QPainter.deviceTransform() when you are working with QT::HANDLE which is an platform dependent handle. Since I wasn't working with a platform dependent handle I shouldn't have called it. It works out when I show the widget, but not when it is embedded in a layout. Then the deviceTransform matrix is different from the normal QPainter.transform() matrix. See also http://doc.qt.io/qt-4.8/qpainter.html#deviceTransform
I am trying to build a python class around QGraphicsRectItem (PySide or PyQt4) that provides mouse interaction through hover-overs, is movable, and re-sizable. I have pretty much everything working except:
For some reason, it seems as if the mouse hover area is not changing when the item is re-sized or moved. I need help solving this issue.
Maybe the problem is caused by inverting the y axis on the QGraphicsView:
QGraphicsView.scale(1,-1)
QGraphicsRectItem class:
class BoxResizable(QtGui.QGraphicsRectItem):
def __init__(self, rect, parent = None, scene = None):
QtGui.QGraphicsRectItem.__init__(self, rect, parent, scene)
self.setZValue(1000)
self._rect = rect
self._scene = scene
self.mouseOver = False
self.resizeHandleSize = 4.0
self.mousePressPos = None
self.mouseMovePos = None
self.mouseIsPressed = False
self.setFlags(QtGui.QGraphicsItem.ItemIsSelectable|QtGui.QGraphicsItem.ItemIsFocusable)
self.setAcceptsHoverEvents(True)
self.updateResizeHandles()
def hoverEnterEvent(self, event):
self.updateResizeHandles()
self.mouseOver = True
self.prepareGeometryChange()
def hoverLeaveEvent(self, event):
self.mouseOver = False
self.prepareGeometryChange()
def hoverMoveEvent(self, event):
if self.topLeft.contains(event.scenePos()) or self.bottomRight.contains(event.scenePos()):
self.setCursor(QtCore.Qt.SizeFDiagCursor)
elif self.topRight.contains(event.scenePos()) or self.bottomLeft.contains(event.scenePos()):
self.setCursor(QtCore.Qt.SizeBDiagCursor)
else:
self.setCursor(QtCore.Qt.SizeAllCursor)
QtGui.QGraphicsRectItem.hoverMoveEvent(self, event)
def mousePressEvent(self, event):
"""
Capture mouse press events and find where the mosue was pressed on the object
"""
self.mousePressPos = event.scenePos()
self.mouseIsPressed = True
self.rectPress = copy.deepcopy(self._rect)
# Top left corner
if self.topLeft.contains(event.scenePos()):
self.mousePressArea = 'topleft'
# top right corner
elif self.topRight.contains(event.scenePos()):
self.mousePressArea = 'topright'
# bottom left corner
elif self.bottomLeft.contains(event.scenePos()):
self.mousePressArea = 'bottomleft'
# bottom right corner
elif self.bottomRight.contains(event.scenePos()):
self.mousePressArea = 'bottomright'
# entire rectangle
else:
self.mousePressArea = None
QtGui.QGraphicsRectItem.mousePressEvent(self, event)
def mouseReleaseEvent(self, event):
"""
Capture nmouse press events.
"""
self.mouseIsPressed = False
self.updateResizeHandles()
self.prepareGeometryChange()
QtGui.QGraphicsRectItem.mouseReleaseEvent(self, event)
def mouseMoveEvent(self, event):
"""
Handle mouse move events.
"""
self.mouseMovePos = event.scenePos()
if self.mouseIsPressed:
# Move top left corner
if self.mousePressArea=='topleft':
self._rect.setTopLeft(self.rectPress.topLeft()-(self.mousePressPos-self.mouseMovePos))
# Move top right corner
elif self.mousePressArea=='topright':
self._rect.setTopRight(self.rectPress.topRight()-(self.mousePressPos-self.mouseMovePos))
# Move bottom left corner
elif self.mousePressArea=='bottomleft':
self._rect.setBottomLeft(self.rectPress.bottomLeft()-(self.mousePressPos-self.mouseMovePos))
# Move bottom right corner
elif self.mousePressArea=='bottomright':
self._rect.setBottomRight(self.rectPress.bottomRight()-(self.mousePressPos-self.mouseMovePos))
# Move entire rectangle, don't resize
else:
self._rect.moveCenter(self.rectPress.center()-(self.mousePressPos-self.mouseMovePos))
self.updateResizeHandles()
self.prepareGeometryChange()
QtGui.QGraphicsRectItem.mousePressEvent(self, event)
def boundingRect(self):
"""
Return bounding rectangle
"""
return self._boundingRect
def updateResizeHandles(self):
"""
Update bounding rectangle and resize handles
"""
self.offset = self.resizeHandleSize*(self._scene.graphicsView.mapToScene(1,0)-self._scene.graphicsView.mapToScene(0,1)).x()
self._boundingRect = self._rect.adjusted(-self.offset, self.offset, self.offset, -self.offset)
# Note: this draws correctly on a view with an inverted y axes. i.e. QGraphicsView.scale(1,-1)
self.topLeft = QtCore.QRectF(self._boundingRect.topLeft().x(), self._boundingRect.topLeft().y() - 2*self.offset,
2*self.offset, 2*self.offset)
self.topRight = QtCore.QRectF(self._boundingRect.topRight().x() - 2*self.offset, self._boundingRect.topRight().y() - 2*self.offset,
2*self.offset, 2*self.offset)
self.bottomLeft = QtCore.QRectF(self._boundingRect.bottomLeft().x(), self._boundingRect.bottomLeft().y(),
2*self.offset, 2*self.offset)
self.bottomRight = QtCore.QRectF(self._boundingRect.bottomRight().x() - 2*self.offset, self._boundingRect.bottomRight().y(),
2*self.offset, 2*self.offset)
def paint(self, painter, option, widget):
"""
Paint Widget
"""
# show boundingRect for debug purposes
painter.setPen(QtGui.QPen(QtCore.Qt.red, 0, QtCore.Qt.DashLine))
painter.drawRect(self._boundingRect)
# Paint rectangle
painter.setPen(QtGui.QPen(QtCore.Qt.black, 0, QtCore.Qt.SolidLine))
painter.drawRect(self._rect)
# If mouse is over, draw handles
if self.mouseOver:
# if rect selected, fill in handles
if self.isSelected():
painter.setBrush(QtGui.QBrush(QtGui.QColor(0,0,0)))
painter.drawRect(self.topLeft)
painter.drawRect(self.topRight)
painter.drawRect(self.bottomLeft)
painter.drawRect(self.bottomRight)
Rest of code for functioning example:
class graphicsScene(QtGui.QGraphicsScene):
def __init__ (self, parent = None):
QtGui.QGraphicsScene.__init__(self, parent)
def setGraphicsView(self, view):
self.graphicsView = view
app = QtGui.QApplication(sys.argv)
app.setStyle('GTK')
mainWindow = QtGui.QMainWindow()
scene = graphicsScene()
scene.setSceneRect(-100,-100, 200, 200)
# Set up view properties
view = QtGui.QGraphicsView()
view.setScene(scene)
view.scale(1,-1)
view.setRenderHint(QtGui.QPainter.Antialiasing)
view.setViewportUpdateMode(QtGui.QGraphicsView.BoundingRectViewportUpdate)
view.setHorizontalScrollBarPolicy ( QtCore.Qt.ScrollBarAlwaysOff )
view.setVerticalScrollBarPolicy ( QtCore.Qt.ScrollBarAlwaysOff )
view.setUpdatesEnabled(True)
view.setMouseTracking(True)
view.setCacheMode(QtGui.QGraphicsView.CacheBackground)
view.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
scene.setGraphicsView(view)
# Add box
BoxResizable(QtCore.QRectF(-50, 50, 100.0, -100.0), parent = None, scene = scene)
mainWindow.setCentralWidget(view)
mainWindow.show()
app.exec_()
app.deleteLater()
sys.exit()
Adding the above class to a QGraphicsScene and subsequent inverted y axis QGraphicsView will produce something like this:
Any help or suggestions are appreciated! Thanks!
Found part of the problem: If I over-ride the shape function (add this function to the class) with:
def shape(self):
path = QtGui.QPainterPath()
path.addRect(self.boundingRect())
return path
The hover area changes size and position with the box sometimes. Acoording to the docs for QGraphicsItem.shape():
The default implementation calls boundingRect() to return a simple
rectangular shape...
Is this a bug in PyQt4?
Second problem: I think that the boundingRect and the shape rectangle need to have positive widths and heights? This is easily done by adding:
self._rect = self._rect.normalized()
to the mouseReleaseEvent function.