Results 1 to 3 of 3

Thread: Drag and Drop problems

  1. #1
    Join Date
    Oct 2009
    Location
    Rio de Janeiro - Brazil
    Posts
    19
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Drag and Drop problems

    Hi people,

    I'm using qt-creator to make a simple application with drag and drop system. The idea is understand the process.

    I create a simple windows with two panels:


    The idea is using drag and drop to change the icons from the panels.

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. QT_BEGIN_NAMESPACE
    11. class QDropEvent;
    12. QT_END_NAMESPACE
    13.  
    14. class MainWindow : public QMainWindow {
    15. Q_OBJECT
    16. public:
    17. MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. protected:
    21. void dragEnterEvent(QDragEnterEvent *event);
    22. void dragMoveEvent(QDragMoveEvent *event);
    23. void dropEvent(QDropEvent *event);
    24. void mousePressEvent(QMouseEvent *event);
    25.  
    26. private:
    27. Ui::MainWindow *ui;
    28. };
    29.  
    30. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

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

    To me is ok, but I've got some errors:

    Qt Code:
    1. ../drag/mainwindow.cpp:: In member function ‘virtual void MainWindow::dragEnterEvent(QDragEnterEvent*)’:
    2. ../drag/mainwindow.cpp:18: error: invalid use of incomplete type ‘struct QDragEnterEvent’
    3. ..drag-build-desktop/../../qtsdk-2010.05/qt/include/QtGui/qwidget.h:88: error: forward declaration of ‘struct QDragEnterEvent’
    4. [...]
    To copy to clipboard, switch view to plain text mode 

    Thanks for all help.

    Junix

  2. #2
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drag and Drop problems

    Just missing #include <QDragEnterEvent>

  3. The following user says thank you to cincirin for this useful post:

    junix (19th May 2011)

  4. #3
    Join Date
    Oct 2009
    Location
    Rio de Janeiro - Brazil
    Posts
    19
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drag and Drop problems

    Great!

    Now is working.

    Thank you cincirin.

Similar Threads

  1. Replies: 2
    Last Post: 13th October 2010, 21:51
  2. Replies: 3
    Last Post: 10th June 2010, 15:13
  3. Replies: 0
    Last Post: 4th May 2010, 10:24
  4. drag and drop problems
    By ianbd in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2008, 20:40
  5. Drag and Drop
    By allensr in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2006, 20:50

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.