PDA

View Full Version : Resizing QGraphicsItems [solved]



pherthyl
18th February 2007, 01:53
Hi,

I'm writing a simple Viso like diagramming program, and I'm having some trouble with QGraphicsView not updating properly when things are resized. At its simplest, I have a rectangular QGraphicsItem, and when the user drags the resize grip in its corner they can resize the rectangle. The relevant methods are below:



void Sticky::mousePressEvent(QGraphicsSceneMouseEvent *event) {
if(sizeGripItem->contains(mapToItem(sizeGripItem, event->pos()))) {
resizing = true;
}

QGraphicsItem::mousePressEvent(event);
}

void Sticky::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
if(resizing) {
setSize(QSize(event->pos().x(), event->pos().y()));
emit(moved(this));
update();
}
else {
emit(moved(this));
QGraphicsItem::mouseMoveEvent(event);
}
}

void Sticky::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
if(resizing) {
rect.setBottomRight(graphWidget->getNearestGridPoint(rect.bottomRight()));
relayout();
emit(moved(this));
// hack to tell the canvas we're at a new size
moveBy(1,1);
moveBy(-1,-1);

scene()->update();
}
resizing = false;
QGraphicsItem::mouseReleaseEvent(event);
}


As you can see, on mouse click, I set a resizing flag, then on mouse move, I set the new size of the component by changing its bounding rectangle. This code has two problems:
Firstly, if I quickly resize the box smaller, artifacts are left on the screen where the box was. So when I set the new size of the boundingrect, the view doesnt refresh properly.
Secondly, when I am done resizing, for some reason the scene doesn't know about the new size of the item, so mouse clicks won't work properly.

These two problems both boil down to the same issue I think, which is that the scene is never informed that my item has changed, so it cannot do things like repaint properly, or update the sizes for mouse click detection.

As you can see in my mouseReleaseEvent, I use the hack of moving the item by one pixel, and moving it back to tell the scene that I've updated the size of the item, but this is definitely not optimal.

Any ideas about how to resize items properly?

jpn
18th February 2007, 01:59
Any ideas about how to resize items properly
Try what's suggested in this post (http://www.qtcentre.org/forum/p-force-redraw-of-every-graphicsitem-post27192/postcount6.html).

pherthyl
18th February 2007, 03:35
Thank you so much. Works great!

elsheikhmh
7th April 2007, 19:46
At its simplest, I have a rectangular QGraphicsItem, and when the user drags the resize grip in its corner they can resize the rectangle.


@pherthyl: how can you place resize grip on you custom subclassed QGraphicsItem?

I'm so sorry for hacking this thread. but I'm overwhelmed.

Thanks,
- Mustafa

thibs
14th April 2017, 16:32
Try what's suggested in this post (http://www.qtcentre.org/forum/p-force-redraw-of-every-graphicsitem-post27192/postcount6.html).

This link doesn't exist anymore. Does anyone knows how to do it?