Results 1 to 5 of 5

Thread: drag&drop from QGraphicsScene to QListWidget

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default drag&drop from QGraphicsScene to QListWidget

    Hi, I am developing a desktop applications which is inherited from QMainWindow and contains several widgets such as QDockWidgets, QListWidgets, QPushButtons, etc.
    I have a QGraphicsView, which includes a QGraphicsScene containing a graphics item which inherits QGraphicsPixmapItem. The scene centers the main window. I want to drag and drop its content into a QListWidget. When dropped, I want to add the graphic item class' text member to the QListWidget. I have the following nonworking piece of code:
    This first part is from my graphics item class:

    Qt Code:
    1. void myGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. if (event->button() != Qt::LeftButton) {
    4. event->ignore();
    5. return;
    6. }
    7.  
    8. setCursor(Qt::ClosedHandCursor);
    9.  
    10. update();
    11.  
    12. }
    13.  
    14. void myGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    15. {
    16. if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
    17. .length() < QApplication::startDragDistance()) {
    18. return;
    19. }
    20.  
    21. QDrag* drag = new QDrag(event->widget());
    22.  
    23. QMimeData* mime = new QMimeData;
    24.  
    25. mime->setImageData(this->getImage());
    26. mime->setText(this->path);
    27.  
    28. drag->setMimeData(mime);
    29.  
    30. drag->setPixmap(this->getPixmap().scaled(30, 40));
    31.  
    32. drag->exec(Qt::CopyAction);
    33.  
    34. setCursor(Qt::OpenHandCursor);
    35.  
    36. update();
    37. }
    38.  
    39.  
    40. void myGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    41. {
    42. setCursor(Qt::OpenHandCursor);
    43.  
    44. update();
    45. }
    To copy to clipboard, switch view to plain text mode 

    The second part is from my widget class, which I have created from its ui file. The ui contains a QListWidget and two QPushButton.

    Qt Code:
    1. class myWidget : public QWidget, public Ui::myWidgetClass
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. myWidget (QWidget *parent = 0);
    7. ~myWidget ();
    8.  
    9. protected:
    10. void dragEnterEvent(QDragEnterEvent *event);
    11. void dragMoveEvent(QDragMoveEvent *event);
    12. void dropEvent(QDropEvent *event);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. myWidget ::myWidget (QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. setupUi(this);
    5. setAcceptDrops(true);
    6. }
    7.  
    8. void myWidget ::dropEvent( QDropEvent *event )
    9. {
    10. if (event->mimeData()->hasImage() && event->mimeData()->hasText())
    11. {
    12. event->acceptProposedAction();
    13.  
    14. QString absolutePath = ;
    15. QListWidgetItem item(event->mimeData()->text());
    16. myListWidget->addItem(&item);
    17. }
    18. else
    19. event->ignore();
    20. }
    21. void ProjectWidget::dragEnterEvent( QDragEnterEvent *event )
    22. {
    23. event->setDropAction(Qt::CopyAction);
    24. event->accept();
    25. }
    26.  
    27. void ProjectWidget::dragMoveEvent( QDragMoveEvent *event )
    28. {
    29. event->setDropAction(Qt::CopyAction);
    30. event->accept();
    31. }
    To copy to clipboard, switch view to plain text mode 

    The problem is, the drag&drop operation never starts. When I double left click(no response when 1 left click) on the graphics item, the cursor changes to Qt::ClosedHandCursor state and keeps the same state while the mouse cursor is on the graphics item(say it is a 400x300 region).
    The list widget item, which is the target widget for the drop action, is on the left most part of the app gui.
    So the drag&drop always fails. The mouse is in Qt::ClosedHandCursor state when I double click on the image and drag anywhere outside while keeping the left button pressed, but never drops the item on the list widget when I release the button.
    What can be the problem?
    Thanks in advance.
    Last edited by hakiim35; 7th July 2010 at 09:00.

Similar Threads

  1. Drag and drop between QListWidget's
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2011, 04:29
  2. Replies: 3
    Last Post: 10th June 2010, 15:13
  3. Replies: 1
    Last Post: 22nd October 2009, 00:02
  4. QListWidget Drag and Drop problem
    By winkle99 in forum Qt Programming
    Replies: 0
    Last Post: 20th October 2009, 20:00
  5. QListWidget/QTreeWidget Drag and Drop
    By hlvietlong in forum Qt Programming
    Replies: 2
    Last Post: 30th June 2009, 18:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.