Results 1 to 4 of 4

Thread: Drag and Drop

  1. #1
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Drag and Drop

    Hey everyone

    I want a picture label that is on top of another bigger picture label to be dragged and dropped.

    so in essence...I want to move a label from one location to another.

    There were some tutorials online but they were too advanced for me.

    Could someone please show/simplify to me how to do this.

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and Drop

    What did you already try?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag and Drop

    I got the following drag and drop code from another program.
    I think I have to mold this code with my program so it can drag picture labels.

    I created my program using Qt designer...so i am a little lost how to change my ui_mainwindow.h so labels can be dragged.

    Please help..
    thanks


    Qt Code:
    1. void DragWidget::dragEnterEvent(QDragEnterEvent *event)
    2. {
    3. if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
    4. if (event->source() == this) {
    5. event->setDropAction(Qt::MoveAction);
    6. event->accept();
    7. } else {
    8. event->acceptProposedAction();
    9. }
    10. } else {
    11. event->ignore();
    12. }
    13. }
    14.  
    15. void DragWidget::dragMoveEvent(QDragMoveEvent *event)
    16. {
    17. if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
    18. if (event->source() == this) {
    19. event->setDropAction(Qt::MoveAction);
    20. event->accept();
    21. } else {
    22. event->acceptProposedAction();
    23. }
    24. } else {
    25. event->ignore();
    26. }
    27. }
    28.  
    29. void DragWidget::dropEvent(QDropEvent *event)
    30. {
    31. if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
    32. QByteArray itemData = event->mimeData()->data("application/x-dnditemdata");
    33. QDataStream dataStream(&itemData, QIODevice::ReadOnly);
    34.  
    35. QPixmap pixmap;
    36. QPoint offset;
    37. dataStream >> pixmap >> offset;
    38.  
    39. QLabel *newIcon = new QLabel(this);
    40. newIcon->setPixmap(pixmap);
    41. newIcon->move(event->pos() - offset);
    42. newIcon->show();
    43. newIcon->setAttribute(Qt::WA_DeleteOnClose);
    44.  
    45. if (event->source() == this) {
    46. event->setDropAction(Qt::MoveAction);
    47. event->accept();
    48. } else {
    49. event->acceptProposedAction();
    50. }
    51. } else {
    52. event->ignore();
    53. }
    54. }
    55.  
    56. void DragWidget::mousePressEvent(QMouseEvent *event)
    57. {
    58. QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
    59. if (!child)
    60. return;
    61.  
    62. QPixmap pixmap = *child->pixmap();
    63.  
    64. QByteArray itemData;
    65. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    66. dataStream << pixmap << QPoint(event->pos() - child->pos());
    67.  
    68. QMimeData *mimeData = new QMimeData;
    69. mimeData->setData("application/x-dnditemdata", itemData);
    70.  
    71. QDrag *drag = new QDrag(this);
    72. drag->setMimeData(mimeData);
    73. drag->setPixmap(pixmap);
    74. drag->setHotSpot(event->pos() - child->pos());
    75.  
    76. QPixmap tempPixmap = pixmap;
    77. QPainter painter;
    78. painter.begin(&tempPixmap);
    79. painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
    80. painter.end();
    81.  
    82. child->setPixmap(tempPixmap);
    83.  
    84. if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction)
    85. child->close();
    86. else {
    87. child->show();
    88. child->setPixmap(pixmap);
    89. }
    90. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and Drop

    Quote Originally Posted by cwnelatury View Post
    I got the following drag and drop code from another program.
    I think I have to mold this code with my program so it can drag picture labels.
    And what doesn't work?

    so i am a little lost how to change my ui_mainwindow.h so labels can be dragged.
    We don't change ui_*.h files. We provide host classes for them.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Drag and Drop QTableWidget in UI file.
    By tpf80 in forum Qt Programming
    Replies: 3
    Last Post: 20th January 2009, 23:02
  2. drag and drop QToolButton in QToolBar
    By NBilal in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 20:11
  3. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 16:56
  4. Drag & Drop when parent and childs accept drops?
    By gri in forum Qt Programming
    Replies: 0
    Last Post: 17th October 2008, 09:00
  5. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 16:41

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.