[Qt-interest] translate vs setPos
Brian McKinnon
bpmckinnon.qt at gmail.com
Mon Dec 6 15:18:04 CET 2010
I do like having the code in the scene... but I'm still having the same
problem with the item jumping back to the start position. I just tried a
new solution that seems to work. I do:
DragableRectangleItem::DragableRectangleItem(const QPointF & pos, QByteArray
& byteArray, QGraphicsItem * parent) :
QGraphicsRectItem(parent)
{
QDataStream dataStream(&byteArray, QIODevice::ReadOnly);
qint32 version;
QRectF rect;
QBrush brush;
QPen pen;
dataStream >> version;
if(version >= 0)
{
dataStream >> rect;
dataStream >> brush;
dataStream >> pen;
}
rect.moveCenter(pos); <--- this is the change
setRect(rect);
setBrush(brush);
setPen(pen);
setFlag(QGraphicsItem::ItemIsMovable, true);
}
Where the moveCenter function puts the rect in the correct position. Is
there any possibility that calling both setRect and setPos in the item's
constructor somehow generates a conflicting state? Where the items position
is correct in the scene but the origin used for dragging is incorrect. That
seems odd... but it's all I've got.
On Wed, Dec 1, 2010 at 6:06 AM, <Oliver.Knoll at comit.ch> wrote:
> On 2010-11-30 Brian Brian McKinnon wrote:
>
> > ...
> > 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.
>
> Okay, so I understand you want to insert some data upon a drag and drop
> operation upon the scene. As a matter of fact I just did the same a couple
> of days ago, and I also struggled with getting the drag and drop support
> work in QGraphicsSCENE: you overwrote the corresponding methods in the
> QGraphicsVIEW, as I understand.
>
> (But I was too lazy to extend from the view, because then I would have had
> to insert custom widgets ("promote", define #include paths etc.) in Qt
> Designer - but this just as a side note).
>
> The CRUCIAL thing - and a bit counterintuitive - is to know that you ALSO
> have to overwrite the
>
> void MyGraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event) {
> if (event->mimeData()->hasUrls()) {
> event->accept(); // THIS IS CRUCIAL: ACCEPT AGAIN!
> }
> }
>
> See the Qt-Interest archive "QGraphicsScene: drag'n'drop onto empty scene?
> (dropEvent() never called) [SOLVED]" on Friday last week for the whole
> story
>
> The explanation is that the DEFAULT implementation of
> QGraphicsScene::dragMoveEvent REJECTS the drop event IF there is no item
> under the mouse! Which is very likely the case, because you just want to
> insert a new item at this empty place ;) Sounds reasonable, but is nowhere
> documented in the Qt docs and you and I are apparently not the first persons
> to stumble across this issue!
>
>
> Add a comment (as I did) here, if you also feel this should be mentioned in
> a special "Drag and drop on QGraphicsScene" chapter:
> http://doc.trolltech.com/4.7/qgraphicsscene.html
>
> (There is a TINY LITTLE "[+] Documentation Feedback" link in
> LIGHT-BRIGHT-GREY ON WHITE colour, with a TINY MICRO-FONT-SIZE of about 4
> px, at the VERY BOTTOM RIGHT of that page ;)
>
>
> You read the explanation here again:
> http://www.qtcentre.org/threads/8022-QGraphicsScene-doesn-t-accept-Drops
>
>
> Anyway, this works for me: in the QGraphicsScene, in the dropEvent()
> method, I get the position of the mouse like this:
>
> void ScreenieGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event)
> {
> ...
> QPointF position = event->scenePos();
> ...
> }
>
> I then do (basically) the following (in some slot which is connected to my
> own scene model):
>
> MyGraphicsPixmapItem *item = new MyGraphicsPixmapItem (...);
> item ->setPos(position);
> m_graphicsScene.addItem(item);
>
> This places the item (a QGraphicsPixmapItem based item) with its top-left
> corner at where the mouse was when dropping the data.
>
> > ...
> > QGraphicsRectItem * rect = new
> > DragableRectangleItem(mapToScene(event- pos()),
>
> QGraphicsRectItem *rect = new DragableRectangleItem(event->scenePos(), ...
>
> (instead of mapToScene(event->pos()).
>
> Should be equivalent here (to my understanding of QGraphicsView/Scene) -
> but maybe it makes the difference already? It works for me...
>
>
> Cheers, Oliver
> --
> Oliver Knoll
> Dipl. Informatik-Ing. ETH
> COMIT AG - ++41 79 520 95 22
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101206/e4fbc41f/attachment.html
More information about the Qt-interest-old
mailing list