Dear Wysota

I over ride the drag and drop function by setting the QTreeWidget class as a parent..

sample code

mainwindow.h
Qt Code:
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QTreeWidgetItem>
  4. #include <QtGui/QMainWindow>
  5. #include <QDropEvent>
  6. namespace Ui
  7. {
  8. class MainWindowClass;
  9. }
  10.  
  11. class MainWindow : public QMainWindow,public QTreeWidget
  12. {
  13. Q_OBJECT
  14.  
  15. public:
  16. MainWindow(QWidget *parent = 0);
  17. ~MainWindow();
  18. bool dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action);
  19. QMimeData *mimeData(const QList<QTreeWidgetItem *> items) const;
  20. void dragEnterEvent(QDragEnterEvent *e);
  21. void dragMoveEvent(QDragMoveEvent *e) ;
  22. private:
  23. Ui::MainWindowClass *ui;
  24.  
  25. };
  26.  
  27. #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. #include <QMessageBox>
  4. MainWindow::MainWindow(QWidget *parent)
  5. : QMainWindow(parent),QTreeWidget(parent), ui(new Ui::MainWindowClass)
  6. {
  7. ui->setupUi(this);
  8. ui->treeWidget->setColumnCount(1);
  9. ui->treeWidget->setAcceptDrops( true );
  10. // The tree supports dragging of its own items
  11. ui->treeWidget->setDragEnabled(true);
  12. QTreeWidgetItem *itemOne = new QTreeWidgetItem(ui->treeWidget);
  13. QTreeWidgetItem *itemTwo = new QTreeWidgetItem(ui->treeWidget);
  14. QTreeWidgetItem *itemThree = new QTreeWidgetItem(ui->treeWidget);
  15. itemOne->setText(0, "John");
  16. itemTwo->setText(0, "Peter");
  17. itemThree->setText(0, "Kumar");
  18. }
  19. QMimeData *MainWindow::mimeData(const QList<QTreeWidgetItem *> items) const
  20. {
  21. QMessageBox::information(0,"",QString("Drag and Drop"));
  22. // Create a QByteArray to store the data for the drag.
  23. QByteArray text;
  24. // Create a data stream that operates on the binary data
  25. QDataStream ds(&text, QIODevice::WriteOnly);
  26. // Add each item's text for col 0 to the stream
  27. for (int i=0;i<items.size();i++)
  28. ds << items.at(i)->text(0);
  29. QMimeData *md = new QMimeData;
  30. // Set the data associated with the mime type foo/bar to ba
  31. md->setData("foo/bar", text);
  32. return md;
  33. }
  34. bool MainWindow::dropMimeData(QTreeWidgetItem *parent, int index, const
  35. QMimeData *data, Qt::DropAction action)
  36. {
  37.  
  38. if (parent) {
  39. // Create a QByteArray from the mimedata associated with foo/bar
  40. QByteArray text = data->data("foo/bar");
  41. // Create a data stream that operates on the binary data
  42. QDataStream ds(&text, QIODevice::ReadOnly);
  43. while (!ds.atEnd()) {
  44. QString str;
  45. // Read a byte from the stream into the string
  46. ds >> str;
  47. // Create a new item that has the item that is dropped on as a parent
  48.  
  49. QTreeWidgetItem *newItem = new QTreeWidgetItem(parent);
  50. newItem->setText(0, str);
  51. }
  52. }
  53. return true;
  54. }
  55. void MainWindow::dragEnterEvent(QDragEnterEvent *e)
  56. {
  57. e->accept();
  58. }
  59.  
  60.  
  61. void MainWindow::dragMoveEvent(QDragMoveEvent *e)
  62. {
  63. e->accept();
  64. }
  65.  
  66. MainWindow::~MainWindow()
  67. {
  68. delete ui;
  69. }
To copy to clipboard, switch view to plain text mode 


I have one doubt .,Mainwindow class having two sub classes.which sub class drag enter & drag move functions it will take..

please clear me

Thanks

Addu R