[Qt-interest] translate vs setPos
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Wed Dec 1 11:06:20 CET 2010
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
More information about the Qt-interest-old
mailing list