PDA

View Full Version : Change QGraphicsScene Rect by dragging QGraphicItems



airglide
5th December 2012, 17:50
Hello everyone,

I've a QGraphicsView and a QGraphicsScene, within this scene are QGraphicsitems.
It sounds quiet trivial but I couldn't manage to change the "size" of the scene by dragging an item at the border of the view (->viewport() ).
I've set the size of the scene because I want that the Items are inserted where a click happens. Otherwise it would insert the first item in the middle...

I tried to override the QGraphicsItem::mouseMoveEvent function and then I tried to check if the scene intersects the item (that the item is at the border and not the whole item is shown... , the view should then add some scrollbars to it (like the default behavior) and should expand the whole thing.


thank you for your help

airglide

airglide
8th December 2012, 18:36
I've solved the problem more or less, I've to optimize it but the code looks like this:
The class Pixmap inherits from QGraphicsPixmapItem (QGraphicsItem)


void PixmapItem::mouseMoveEvent(QGraphicsSceneMouseEven t *event)
{
QRectF theRect = QRectF(scenePos(), boundingRect().size());
QRectF intersected = QRectF(theRect.intersected(scene()->sceneRect()));
if(intersected != theRect)
{
//the item is outside of sceneRect
if(theRect.top() != intersected.top())
{
//is at bottom
if(theRect.right() != intersected.right())
{
//is right
qDebug() << "top-right";
QRectF mRect = scene()->sceneRect();
mRect.adjust(0,-50,50,0);
scene()->setSceneRect(mRect);
}else{
if(theRect.left() != intersected.left())
{
//is left
qDebug() << "top-left";
QRectF mRect = scene()->sceneRect();
mRect.adjust(-50,-50,0,0);
scene()->setSceneRect(mRect);
}else{
//only top
qDebug() << "top";
QRectF mRect = scene()->sceneRect();
mRect.adjust(0,-50,0,0);
scene()->setSceneRect(mRect);
}
}
}else{

if(theRect.bottom() != intersected.bottom())
{
//is at bottom
if(theRect.right() != intersected.right())
{
//is right
qDebug() << "bottom-right";
QRectF mRect = scene()->sceneRect();
mRect.adjust(0,0,50,50);
scene()->setSceneRect(mRect);
}else{
if(theRect.left() != intersected.left())
{
//is left
qDebug() << "bottom-left";
QRectF mRect = scene()->sceneRect();
mRect.adjust(-50,0,0,50);
scene()->setSceneRect(mRect);
}else{
//only bottom
qDebug() << "bottom";
QRectF mRect = scene()->sceneRect();
mRect.adjust(0,0,0,50);
scene()->setSceneRect(mRect);
}
}
}else{
if(theRect.right() != intersected.right())
{
qDebug() << "right";
//is right
QRectF mRect = scene()->sceneRect();
mRect.adjust(0,0,50,0);
scene()->setSceneRect(mRect);
}else{
if(theRect.left() != intersected.left())
{
qDebug() << "left";
//is left
QRectF mRect = scene()->sceneRect();
mRect.adjust(-50,0,0,0);
scene()->setSceneRect(mRect);
}}

}
}
}else{
qDebug() << "no intersection";
}
QGraphicsPixmapItem::mouseMoveEvent(event);
}