Results 1 to 1 of 1

Thread: drag and drop QLabels

  1. #1
    Join Date
    Jun 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default drag and drop QLabels

    Hello,

    I'm trying to understand a strange problem, if it is a problem, when doing a drag and drop over QLables in a QFrame.
    I'm using the example of Qt about the drag and drop "Draggable Icons".
    I modified just a little bit the code in the constructor DragWidget :: DragWidget(QWidget *parent) and in the
    void DragWidget::mousePressEvent(QMouseEvent *event) method.

    In the constructor I've put this 3 lines for each label:

    boatIcon->setObjectName(QString("BOAT"));

    carIcon->setObjectName(QString("CAR"));

    houseIcon->setObjectName(QString("HOUSE"));

    In the mouse handler method I've put this line:

    qDebug("child name %s", child->objectName().toLatin1().data());

    So as you see nothing special. But when I'm pressing with the mouse over the labels there is no name object, more precisely qDebug shows only the first time the name and after the objetName() returns empty string ....

    Here is the entire code of the modified example:

    dragwidget.h
    Qt Code:
    1. #ifndef DRAGWIDGET_H
    2. #define DRAGWIDGET_H
    3.  
    4. #include <QFrame>
    5.  
    6. QT_BEGIN_NAMESPACE
    7. class QDropEvent;
    8. QT_END_NAMESPACE
    9.  
    10. //! [0]
    11. class DragWidget : public QFrame
    12. {
    13. public:
    14. DragWidget(QWidget *parent=0);
    15.  
    16. protected:
    17. void dragEnterEvent(QDragEnterEvent *event);
    18. void dragMoveEvent(QDragMoveEvent *event);
    19. void dropEvent(QDropEvent *event);
    20. void mousePressEvent(QMouseEvent *event);
    21. };
    22. //! [0]
    23.  
    24. #endif
    To copy to clipboard, switch view to plain text mode 

    dragwidget.cpp

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "dragwidget.h"
    4.  
    5. //! [0]
    6. DragWidget::DragWidget(QWidget *parent)
    7. : QFrame(parent)
    8. {
    9. setMinimumSize(200, 200);
    10. setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
    11. setAcceptDrops(true);
    12.  
    13. QLabel *boatIcon = new QLabel(this);
    14. boatIcon->setObjectName(QString("BOAT")); //<---- here
    15. boatIcon->setPixmap(QPixmap(":/images/boat.png"));
    16. boatIcon->move(20, 20);
    17. boatIcon->show();
    18. boatIcon->setAttribute(Qt::WA_DeleteOnClose);
    19.  
    20. QLabel *carIcon = new QLabel(this);
    21. carIcon->setObjectName(QString("CAR")); //<---- here
    22. carIcon->setPixmap(QPixmap(":/images/car.png"));
    23. carIcon->move(120, 20);
    24. carIcon->show();
    25. carIcon->setAttribute(Qt::WA_DeleteOnClose);
    26.  
    27. QLabel *houseIcon = new QLabel(this);
    28. houseIcon->setObjectName(QString("HOUSE")); //<---- here
    29. houseIcon->setPixmap(QPixmap(":/images/house.png"));
    30. houseIcon->move(20, 120);
    31. houseIcon->show();
    32. houseIcon->setAttribute(Qt::WA_DeleteOnClose);
    33. }
    34. //! [0]
    35.  
    36. void DragWidget::dragEnterEvent(QDragEnterEvent *event)
    37. {
    38. if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
    39. if (event->source() == this) {
    40. event->setDropAction(Qt::MoveAction);
    41. event->accept();
    42. } else {
    43. event->acceptProposedAction();
    44. }
    45. } else {
    46. event->ignore();
    47. }
    48. }
    49.  
    50. void DragWidget::dragMoveEvent(QDragMoveEvent *event)
    51. {
    52. if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
    53. if (event->source() == this) {
    54. event->setDropAction(Qt::MoveAction);
    55. event->accept();
    56. } else {
    57. event->acceptProposedAction();
    58. }
    59. } else {
    60. event->ignore();
    61. }
    62. }
    63.  
    64. void DragWidget::dropEvent(QDropEvent *event)
    65. {
    66. if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
    67. QByteArray itemData = event->mimeData()->data("application/x-dnditemdata");
    68. QDataStream dataStream(&itemData, QIODevice::ReadOnly);
    69.  
    70. QPixmap pixmap;
    71. QPoint offset;
    72. dataStream >> pixmap >> offset;
    73.  
    74. QLabel *newIcon = new QLabel(this);
    75. newIcon->setPixmap(pixmap);
    76. newIcon->move(event->pos() - offset);
    77. newIcon->show();
    78. newIcon->setAttribute(Qt::WA_DeleteOnClose);
    79.  
    80. if (event->source() == this) {
    81. event->setDropAction(Qt::MoveAction);
    82. event->accept();
    83. } else {
    84. event->acceptProposedAction();
    85. }
    86. } else {
    87. event->ignore();
    88. }
    89. }
    90.  
    91. //! [1]
    92. void DragWidget::mousePressEvent(QMouseEvent *event)
    93. {
    94. QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
    95. if (!child)
    96. return;
    97.  
    98. qDebug("child name %s", child->objectName().toLatin1().data());
    99.  
    100. QPixmap pixmap = *child->pixmap();
    101.  
    102. QByteArray itemData;
    103. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    104. dataStream << pixmap << QPoint(event->pos() - child->pos());
    105. //! [1]
    106.  
    107. //! [2]
    108. QMimeData *mimeData = new QMimeData;
    109. mimeData->setData("application/x-dnditemdata", itemData);
    110. //! [2]
    111.  
    112. //! [3]
    113. QDrag *drag = new QDrag(this);
    114. drag->setMimeData(mimeData);
    115. drag->setPixmap(pixmap);
    116. drag->setHotSpot(event->pos() - child->pos());
    117. //! [3]
    118.  
    119. QPixmap tempPixmap = pixmap;
    120. QPainter painter;
    121. painter.begin(&tempPixmap);
    122. painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
    123. painter.end();
    124.  
    125. child->setPixmap(tempPixmap);
    126.  
    127. if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction)
    128. child->close();
    129. else {
    130. child->show();
    131. child->setPixmap(pixmap);
    132. }
    133. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp

    Qt Code:
    1. #include <QApplication>
    2. #include <QHBoxLayout>
    3. #include "dragwidget.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. Q_INIT_RESOURCE(draggableicons);
    8.  
    9. QApplication app(argc, argv);
    10.  
    11. QWidget mainWidget;
    12. QHBoxLayout *horizontalLayout = new QHBoxLayout;
    13. horizontalLayout->addWidget(new DragWidget);
    14. horizontalLayout->addWidget(new DragWidget);
    15.  
    16. mainWidget.setLayout(horizontalLayout);
    17. mainWidget.setWindowTitle(QObject::tr("Draggable Icons"));
    18. mainWidget.show();
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    If someone has an explanation, I'll be happy to hear it

    Best regards,

    Anton

    PS. The images needed in this example are like attached files, you should put them in a "image" directory in the directory where the source files for this example are.
    Attached Images Attached Images

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. Replies: 1
    Last Post: 8th January 2009, 17:40
  3. drag and drop QToolButton in QToolBar
    By NBilal in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 20:11
  4. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 16:56
  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
  •  
Qt is a trademark of The Qt Company.