gsgx
gsgx

Reputation: 12249

How to make a qwidget know when another qwidget is above it

I have a lot of stationary qwidgets on the screen and I have a lot of other widgets that I can drag around the screen. When a widget is dragged on top of the stationary widget, the stationary widget needs to execute some code. I can't figure out how the stationary object can know there is a widget on top of it and know which widget it is.

EDIT: The object that I'm dragging is being dragged by functions I created myself, not Qt Drag functions. This is what I use

void Piece::mouseMoveEvent(QMouseEvent *event) 
{ 
     if(event->buttons() == Qt::LeftButton && turn == color) 
     { 
          x = event->globalX()-18;
          y = event->globalY()-18;
          move(x,y);
     }
}

Will the dropEvent still work using this method? I tried making one but when I dropped the widget on top of the stationary widget the dropEvent was never entered.

Upvotes: 0

Views: 195

Answers (1)

laurent
laurent

Reputation: 90873

You need to make the stationary object accept drop events using setAcceptDrops(true), then implement these events to execute some code when the other widget is dragged or dropped:

void dragMoveEvent(QDragMoveEvent* event);
void dragEnterEvent(QDragEnterEvent* event);
void dropEvent(QDropEvent* event);

Upvotes: 2

Related Questions