1

I build a widget which accepts drag&drop as follows:

MyWidget::MyWidget( QWidget *p_parent ) : QFrame( p_parent ) { setAcceptDrops( true ); m_layout = new QLayout( this ); //this is layout of my custom widget layout->setSpacing( 0 ); m_indicator = new QWidget( this ); m_indicator->setObjectName( "indicator" ); m_indicator->setFixedWidth( 5 ); layout->addWidget( m_indicator ); .... .... } void MyWidget::dragMoveEvent( QDragMoveEvent *p_event ) { p_event->acceptProposedAction(); } 

My main job is to insert the dragged object into the layout, therefore, I should identify which item is under the mouse, and do my job after that. In function dragMoveEvent above, I can do p_event->pos() to get the position of the mouse. But QLayout does not have method itemAt(int x, int y). What should I do now?

1
  • @hyde: thank you, I just too focused on the layout, and forget about the parent widget :) Commented Apr 20, 2020 at 15:17

1 Answer 1

2

Layouts can't do that, but fortunately the parent widget can. Example code (untested):

void MyWidget::dragMoveEvent( QDragMoveEvent *p_event ) { auto *widget = childAt(p_event->pos()); if(widget) { .... } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.