qt - QGraphicsItem validating a position change -
i have custom qgraphicsitem implementation. need able limit item can moved - i,e. restrict area. when checked qt documentation suggested:
qvariant component::itemchange(graphicsitemchange change, const qvariant &value) { if (change == itempositionchange && scene()) { // value new position. qpointf newpos = value.topointf(); qrectf rect = scene()->scenerect(); if (!rect.contains(newpos)) { // keep item inside scene rect. newpos.setx(qmin(rect.right(), qmax(newpos.x(), rect.left()))); newpos.sety(qmin(rect.bottom(), qmax(newpos.y(), rect.top()))); return newpos; } } return qgraphicsitem::itemchange(change, value); }
so basically, check position passed itemchange, , if don't it, change , return new value.
seems simple enough, except doesn't work. when checked call stack see itemchange being called qgraphicsitem::setpos, doesn't @ return value. there no purpose me returning changed position, no 1 looking @ it. see code qgraphicsitem.cpp
// notify item position changing. const qvariant newposvariant(itemchange(itempositionchange, qvariantfromvalue<qpointf>(pos))); qpointf newpos = newposvariant.topointf(); if (newpos == d_ptr->pos) return; // update , repositition. d_ptr->setposhelper(newpos); // send post-notification. itemchange(qgraphicsitem::itempositionhaschanged, newposvariant); d_ptr->sendsceneposchange();
any suggestions? hoping avoid re-implementing whole click , drag behavior myself using mouse-down mouse-move etc..., suppose have if can't find better idea.
i didn't try looks me checking return position. returned restricted position used in constructor of newposvariant
converted newpos
. it's used set position of item if it's different current one.
Comments
Post a Comment