PDA

View Full Version : function dropEvent(QGraphicsSceneDragDropEvent *event) doesn't work...



Mariusz_1990
18th December 2013, 23:26
Hi All
I have problem with function dropEvent(QGraphicsSceneDragDropEvent *event) when I wanna drop object type QGraphicsItem.
this is my part code

QPixmap dwuMasztowiec;
dwuMasztowiec = QPixmap("../Statki/images/dwu.png");
statek_1 = scene->addPixmap(dwuMasztowiec);
statek_1->setPos(277,405);
statek_1->setFlag(QGraphicsItem::ItemIsMovable);

I have several objects type statek (statek=ship). I wanna drag this ships somewhere in another place that I can drop and read position this ship.

I set flag (QGraphicsItem::ItemIsMovable) so I can drag elements but I haven't ideas how drop and read postion ship.

I used function (which is below) from example robot from Qt but doesn't work


void Plansza::dropEvent(QGraphicsSceneDragDropEvent *event)
{
if (event->mimeData()->hasImage()) {
dragOver = false;
pixmap = qVariantValue<QPixmap>(event->mimeData()->imageData());
update();
} else {
Plansza::dropEvent(event);
}
}

If I explained wrong my example , I will tell and show more details.

I please about support
Thanks

anda_skoa
19th December 2013, 10:31
Drop is something you drag in from outside the view. What you seem to want is repositioning items per mouse.
That should already be possible with the Movable flag. Or do you want to drag an item from one view to another?

Cheers,
_

Mariusz_1990
19th December 2013, 16:43
Exactly I do game in ships and I wanna drag ships on Scene.The drag elements work properly because I added flag (QGraphicsItem::ItemIsMovable). I wanna write function (dropEvent) where I will implement:
- read postion ships
- align ships to the grid
My problem is it that I can't capture event "drop".
I think that now I explained a little more about my problem :)

anda_skoa
20th December 2013, 10:00
Have you enabled drop support on the view? I.e. called setAcceptDrops(true)?

Can you see other drops, e.g. a file list from the file manager?

Btw, you have an infinite recursion in your code, you are calling the method again in the else branch.

See also http://qt-project.org/doc/qt-4.8/graphicsview-dragdroprobot.html

Cheers,
_

Mariusz_1990
21st December 2013, 13:19
Yes I enabled drop support by function setAcceptDrops(true).
I saw example with Robot earlier but I don't understand a little this example...
if I have wrote function dropEvent it I must have also function dragEvent ? or only need to have set flag (QGraphicsItem::ItemIsMovable) ?
Maybe someone has another example similar to Robot but that elements graphics were load to type QGraphicsItem?

anda_skoa
21st December 2013, 13:45
dropEvent() should be fine.

Have you checked if your dropEvent is called by dropping a file list or something else as suggested?

Item moving is, as I said before, unrelated to drag&drop.

Cheers,
_

Mariusz_1990
21st December 2013, 20:48
I will say truly that I can't check call by dropping a file list or something else...

this function doesnt working
void Plansza::dropEvent(QGraphicsSceneDragDropEvent *event)
{
if (event->mimeData()->hasImage()) {
dragOver = false;
pixmap = qVariantValue<QPixmap>(event->mimeData()->imageData());
update();
} else {
Plansza::dropEvent(event);
}
}

don't capture type QgraphicsItem why??
Maybe this function work properly but I have to write "connect" where signal will be function dropEvent?

anda_skoa
22nd December 2013, 11:20
I will say truly that I can't check call by dropping a file list or something else...

Why? Do you have no drag enabled program on the machine that you run this on?



this function doesnt working
void Plansza::dropEvent(QGraphicsSceneDragDropEvent *event)
{
if (event->mimeData()->hasImage()) {
dragOver = false;
pixmap = qVariantValue<QPixmap>(event->mimeData()->imageData());
update();
} else {
Plansza::dropEvent(event);
}
}


Is it being called?
Btw, you still have the infinite recursion in there.



don't capture type QgraphicsItem why??

You are checking for image.


Maybe this function work properly but I have to write "connect" where signal will be function dropEvent?
No, dropEvent is an event handler method, not a slot.

Cheers,
_

Mariusz_1990
22nd December 2013, 11:52
Ok I understood a little... but please maybe help me write this function that work properly? I want read ship postion, when ship will drop. I really have not idea that this work work properly...

anda_skoa
22nd December 2013, 13:06
Your problem description is too abstract, i.e. there is no indication what your problem actually is.

Is your dropEvent() method being called? I.e. if you put some qDebug() output in there, so you see that output?
You can try using a drag from a different application, assuming you have any other program capable of drag&drop running on the same machine. If you run your program on the development machine than it is likely that you have a file manager there.

Cheers,
_

Mariusz_1990
22nd December 2013, 14:43
Probably We don't understand. How I wrote earlier, I wanna only read postion my ships when I am dropping somewhere on SCENE in my program. I can't drag ships outside SCENE. When I added qDebug() to function dropEvent I didn't see any communique about captured image (ships) by function dropEvent.

anda_skoa
22nd December 2013, 15:06
When I added qDebug() to function dropEvent I didn't see any communique about captured image (ships) by function dropEvent.

Let me rephrase. If you do this:



void Plansza::dropEvent(QGraphicsSceneDragDropEvent *event)
{
qDebug() << Q_FUNC_INFO;
}

do you get that output?
If not, where does the drag come from, i.e. where does the drag mouse movement start?
If you drag files from the file manager (assuming the system has a file manager), does the cursor change to indicate that drop is allowed when you move over your view?

Cheers,
_

Mariusz_1990
22nd December 2013, 18:54
after added your entry (qDebug() << Q_FUNC_INFO) I still nothing get on the output :/ I start drag element from Scene and want drop also on Scene. When I drag files from the file manager cursor change sign and I can't drop file on the Scene. I have on the Scene 2 board 300px/300px and I want secure second board that someone can't drop ship on her so I need know position ship after drop ship that I can write function which will secure drop on her.

anda_skoa
22nd December 2013, 20:45
after added your entry (qDebug() << Q_FUNC_INFO) I still nothing get on the output :/

Ok, that is not good. that should have worked for the file drop. Oh, I just had a thought.
Your dropEvent(), in which class is it? The view or the scene?



I start drag element from Scene and want drop also on Scene.

Are those two different scenes or one scene in two different views?

Cheers,
_

Mariusz_1990
22nd December 2013, 23:05
I have one scene.
I attache file statek.pro with my project. Look at this in free moment :)9877

Mariusz_1990
23rd December 2013, 06:38
I will return into question about " Do I need implement function dragEnterEvent,DragMoveEvent?" because I see in documentation QT that they wrote

void QGraphicsItem::dropEvent ( QGraphicsSceneDragDropEvent * event ) [virtual protected]

This event handler, for event event, can be reimplemented to receive drop events for this item. Items can only receive drop events if the last drag move event was accepted.

so How I have interpret this description?