JustinBlaber
JustinBlaber

Reputation: 4650

Reposition graphics items on window resize

Ok, I basically have randomly placed graphics items on a graphics scene. I open a window and display these items. When I resize the window, I want this items to stay the same size but reposition proportionally to the window size. How I did this was I subclassed the graphics view and then when there was a resize event, it sent the resize object to my graphicsscene object. I was then able to do something like this:

double scaleX = double(event->size().width())/double(event->oldSize().width());
double scaleY = double(event->size().height())/double(event->oldSize().height());

Then I used these values to do this:

derivedPointItem->setPos( derivedPointItem->pos().x() * scaleX,
                          derivedPointItem->pos().y() * scaleY );

This works ok, but it's not quite right if I resize the window really small or large. I think the problem is that the graphicsscene rect and the graphics view rect are not the same or something. Furthermore, I have a background which resizes to window size:

void roiwindow::drawBackground( QPainter* painter, const QRectF& rect )
{
    this->setSceneRect(0,0,rect.width(),rect.height());
    painter->save();
    painter->drawImage(rect, *refimage);
    painter->restore();
}

This is something I need in the program I'm writing. Also, it allows me to see if the resizing works. I basically can set the background to be a polygon, then place points on the edges of the polygon. When I resize the image to be very large or small the points are no longer on the vertices of the polygon althought they are somewhat close. Anyone know a better way to do this or a way to fix it? Thanks.

EDIT: Here's the project I'm working on:

dropbox.com/s/myxi8kvdl7x9ye2/ncorr.tar.gz

Upvotes: 0

Views: 588

Answers (1)

JustinBlaber
JustinBlaber

Reputation: 4650

This method actually works (although I'm not sure if it's the best method to use...). Anyway, it ended up being a coding error due to the setPos() function. The setPos() function in my program worked by specifying the coordinates of the top left corner of the image rather than the center. This ended up causing some errors.

Upvotes: 1

Related Questions