I'm trying to give a user the ability to drag items from the QTreeWidget and move it to the QGraphicsView - the icon of current item must be transformated to the QGraphicsItem's pixmap, the text of current item must become an remark to that graphics item. But currently I coudn't catch the drop event - in the following code I had reimplemented "dragEnter", "drop" and "mousePressed" event, but the Drop event's listener invokes only one time and mousePressed event isn't tracked. Maybe someone know how to correctly handle drag and drop between different widgets on the same form?
Here are sample of my code:
Qt Code:
  1. void addcondition::dragEnterEvent ( QDragEnterEvent * event )
  2. {
  3. event->acceptProposedAction();
  4. }
  5.  
  6. void addcondition::dropEvent ( QDropEvent * event )
  7. {
  8. if(m_ui->toolBox->currentIndex()==0)
  9. //just current page of toolbox item - this isn't important
  10. {
  11. //QGraphicsScene scene has declared in the header file
  12. scene.addText(event->mimeData()->text());
  13. m_ui->gcond->setScene(&scene);
  14. m_ui->gcond->show();
  15. }
  16. event->acceptProposedAction();
  17. }
  18. void addcondition::mousePressEvent( QMouseEvent * event)
  19. {
  20. if (event->button() == Qt::LeftButton)
  21. {
  22. QDrag *drag = new QDrag(this);
  23. QMimeData *mimeData = new QMimeData;
  24. if(m_ui->toolBox->currentIndex()==0)
  25. mimeData->setText(m_ui->cond_view->currentItem()->text(0));
  26. drag->setMimeData(mimeData);
  27. Qt::DropAction dropAction = drag->exec();
  28. qDebug()<<"mouse event detected";
  29. //this event will never be tracked. I don't know, why
  30. }
  31. }
To copy to clipboard, switch view to plain text mode