[Qt-interest] translate vs setPos
Brian McKinnon
bpmckinnon.qt at gmail.com
Tue Nov 30 18:31:13 CET 2010
On Tue, Nov 30, 2010 at 1:17 PM, <Oliver.Knoll at comit.ch> wrote:
> On 2010-11-30 Brian Brian McKinnon wrote:
>
> > Hi everyone,
> >
> > I'm having some trouble with creating objects and setting there
> > position. I use the code below to create an object after the completion
> of a drag operation.
> >
> > DragableRectangleItem::DragableRectangleItem(const QPointF & pos, ...);
> > ....
> > //setPos(pos.x(), pos.y()); // <- Doesn't work
> > //moveBy(pos.x(), pos.y()); // <- Doesn't work
> > //translate(pos.x(), pos.y()); // <- Works } At the bottom I have
>
> > The problem is that once I click and move the item positioned with
> > setPos or moveBy it jumps back to the origin of the rect completely
>
> Well, where does the parameter 'pos' come from? From the mouse
> event->pos()? Note that this will give you the *parent* mouse coordinate, so
> (I assume, did not verify) for a mouse event in a QGraphicsScene the parent
> would be the underlying item! So you probably want to initialize the
> parameter 'pos' with something like event->scenePos(), such that you get the
> global scene coordinates instead (because that is where the user would
> expect the item to be placed ;)
>
> Does that help?
>
I create it from the overloaded drop event in my graphics view. I wasn't
able to get the drag event inside the scene to activate so I drop it in from
the scene.
void ShapeGraphicsView::dropEvent(QDropEvent * event)
{
if(event->mimeData()->hasFormat(typeid(DragableRectangleItem).name()))
{
QGraphicsRectItem * rect = new
DragableRectangleItem(mapToScene(event->pos()),
event->mimeData()->data(typeid(DragableRectangleItem).name()));
rect->setFlag(QGraphicsItem::ItemIsMovable, true);
scene()->addItem(rect);
event->acceptProposedAction();
}
else
QGraphicsView::dropEvent(event);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101130/0f155414/attachment.html
More information about the Qt-interest-old
mailing list