qt - Zoom in scaled down QPixmap: can't restore original size -
i making application need draw figures (i.e. rectangles) above picture , resize whole scene. i'm using qgraphicsview
, qgraphicsscene
. can't figure out why when using fitintoview()
unable draw figures on top of picture (they drawn behind picture or outside bounds).
now i'm using qpixmap.scaled()
make image fit in qgraphicsscene
. works fine except when need display big image , zoom in. because scaled image fit, became smaller , when call qgraphicsview.scale()
image scales small 1 , can't find way "scale back" image original size.
in zoom part of code tried replace pixmap not scaled copy can't scale same proportions figures scaling. appreciated!
here load image , scale it
qpixmap pix; pix = pixmap->scaled(wid, hei,qt::keepaspectratio, qt::smoothtransformation); scn = new qgraphicsscene(pw); pw->setscene(scn); p = new qgraphicspixmapitem(pix); p->setpixmap(pix); scn->additem(p);
i draw figures this
rect = new qgraphicsrectitem(x,y,w,h); rect->setpen(qpen(getcolor(), 1, qt::solidline, qt::flatcap)); scn->additem(rect);
and here's how zoom
pw->scale(scalefactor_,scalefactor_);
update: have qwidget
kind of toolbar on top, rest of free space filled custom qgraphicsview
. derived class grabbing mouse events, there's nothing more overrides of event handlers. i'm adding paintergraphicsview in widget's constructor:
pw = new paintergraphicsview(); ui->gridlayout_3->addwidget(pw);
i'm recreating scene every time load new image it:
scn = new qgraphicsscene(pw); pw->setscene(scn); qrect r = qrect(0,0, pw->width()-5, pw->height()-5); scn->setscenerect(r);
after loaded image can draw figures on top of it, looks this:
and scaled contents this:
as can see, figures scaled pixmap , how need work. thing have fix low quality of scaled pixmap. said, tried replace pixmap original 1 in zoomin method, can't saving figures' position.
void drawerwidget::on_btzoom_clicked() { p->setpixmap(pix); //p qgraphicspixmapitem, pix original pixmap p->setscale(0.5); //how can scale pixmap figures?? how calculate required scale? pw->scale(scalefactor_,scalefactor_); }
update 2:
a short explanation on drawing figures method: (though don't think issue, i'm interested there way scale qgraphicspixmapitem using setscale() fit in qgraphicsview size)
i'm getting start_
, end_
coordinates mouse events arguments (qmouseevent *e)
end_=pw->maptoscene(e->pos()); start_=pw->maptoscene(e->pos());
i add figure on mousepress:
rect = new qgraphicsrectitem(start_.x(),start_.y(),1,1); scn->additem(rect);
and redraw figure on mouse move
rect->setrect(graphics::getfigure(start_.x(),start_.y(),end_.x(),end_.y()));
getfigure method calculating figure's rect, here's main part of it:
static qrectf getfigure(double startx, double starty, double finalx, double finaly) { qrectf shape; shape.setx(startx); shape.sety(starty); shape.setwidth(qfabs(finalx - startx)); shape.setheight(qfabs(finaly - starty)); return shape; }
the scaling operation destructive, once scale down end less data started with.
if want able restore original size keep copy of original pixmap.
you can create qgraphicspixmapitem
original, non-scaled pixmap , use qgraphicspixmapitem::setscale(qreal)
change size on level graphics item, not on level pixmap data. way scaling should not destructive, , should not lose quality if scale original size.
if want scale multiple items (and not run different transformation origin problems) have few options:
- scale entire view - scale everything
- add items children of parent item, scaling parent scale children well
item transformations accumulate parent child
- add items group using
createitemgroup()
:
the items reparented group, , positions , transformations mapped group
Comments
Post a Comment