Results 1 to 2 of 2

Thread: to use drag & drop on QListWidget

  1. #1
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default to use drag & drop on QListWidget

    Hello everyone,
    I'm using Qt 4.8.3 and Qt Creator 2.5.2 (maybe there is a bug because I do not compile in the resource file *. QRC, I have to do it manually from the command line dos) in Windows XP.
    I created a class ListaElementi by subclassing QListWidget in which the figures are listed in svg format. An example to understand is the following code:
    Qt Code:
    1. item = new QListWidgetItem(QIcon("ostacolo.svg"), "Ostacolo tipo 1", this);
    2. insertItem(1, item);
    To copy to clipboard, switch view to plain text mode 
    So far so good, I show you a list of figures. The project continues dragging and dropping onto my scene class (derived from QScene) one of the figures taken from ListaElementi. Even so good.
    My problem is to make sure that when I select the figure from ListaElementi and drag near to the mouse cursor would like to see the figure selected in such a way so that I can see her standing before release (drop) on the scene.
    Can you tell me how to write the code?
    To simplify things I reproduce below part of the code of the drag ListaElementi:
    Qt Code:
    1. void ListaElementi::mousePressEvent(QMouseEvent *event)
    2. {
    3. // si evidenzia l'elemento selezionato
    4. QListWidget::mousePressEvent(event);
    5.  
    6. //qDebug() << "ListaElementi::mousePressEvent";
    7.  
    8. // creo uno stream di dato contenente il nome del
    9. // item selezionato per usarlo nel drag&drop
    10. if(currentItem())
    11. {
    12. QByteArray dato;
    13. QDataStream stream(&dato, QIODevice::WriteOnly);
    14. stream << currentItem()->text();
    15. QMimeData *mime = new QMimeData;
    16. mime->setData("elementoListaSelez", dato);
    17.  
    18. QDrag *trascina = new QDrag(this);
    19. trascina->setMimeData(mime);
    20. trascina->setObjectName(currentItem()->text());
    21. trascina->setHotSpot(event->pos());
    22. trascina->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
    23. }
    24. }
    25.  
    26. void ListaElementi::dragEnterEvent(QDragEnterEvent *event)
    27. {
    28. //qDebug() << "ListaElementi::dragEnterEvent";
    29. QListWidget::dragEnterEvent(event);
    30.  
    31. // controlla che il dato da passare è un "elementoListaSelez"
    32. if(event->mimeData()->hasFormat("elementoListaSelez"))
    33. {
    34. // il dato non è preso dalla ListaElementi
    35. event->acceptProposedAction();
    36. }
    37. else
    38. {
    39. // l'elemnto non è del tipo "elementoListaSelez"
    40. event->ignore();
    41. }
    42. }
    43.  
    44. void ListaElementi::dragMoveEvent(QDragMoveEvent *e)
    45. {
    46. //qDebug() << "ListaElementi::dragMoveEvent";
    47. QListWidget::dragMoveEvent(e);
    48.  
    49. // controlla che il dato da passare è un "elementoListaSelez"
    50. if(e->mimeData()->hasFormat("elementoListaSelez"))
    51. {
    52. // il dato non è preso dalla ListaElementi
    53. e->acceptProposedAction();
    54. }
    55. else
    56. {
    57. // l'elemnto non è del tipo "elementoListaSelez"
    58. e->ignore();
    59. }
    60. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up [RESOLVED] Re: to use drag & drop on QListWidget

    Ok all resolved, to see Qt italia forum: http://www.qt-italia.org/forum/viewtopic.php?f=6&t=829

Similar Threads

  1. Drag and Drop items in QListWidget
    By tommynator128 in forum Qt Programming
    Replies: 4
    Last Post: 10th March 2011, 12:09
  2. Drag and drop between QListWidget's
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2011, 05:29
  3. drag&drop from QGraphicsScene to QListWidget
    By hakiim35 in forum Qt Programming
    Replies: 4
    Last Post: 7th July 2010, 12:29
  4. Replies: 3
    Last Post: 10th June 2010, 16:13
  5. Drag from QTreeWidget and drop in QListWidget
    By WXNSNW in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2008, 01:15

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.