Dear Wysota
I over ride the drag and drop function by setting the QTreeWidget class as a parent..
sample code
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QTreeWidgetItem>
#include <QtGui/QMainWindow>
#include <QDropEvent>
namespace Ui
{
class MainWindowClass;
}
{
Q_OBJECT
public:
~MainWindow();
QMimeData *mimeData
(const QList<QTreeWidgetItem
*> items
) const;
private:
Ui::MainWindowClass *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QTreeWidgetItem>
#include <QtGui/QMainWindow>
#include <QDropEvent>
namespace Ui
{
class MainWindowClass;
}
class MainWindow : public QMainWindow,public QTreeWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
bool dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action);
QMimeData *mimeData(const QList<QTreeWidgetItem *> items) const;
void dragEnterEvent(QDragEnterEvent *e);
void dragMoveEvent(QDragMoveEvent *e) ;
private:
Ui::MainWindowClass *ui;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow
::MainWindow(QWidget *parent
){
ui->setupUi(this);
ui->treeWidget->setColumnCount(1);
ui->treeWidget->setAcceptDrops( true );
// The tree supports dragging of its own items
ui->treeWidget->setDragEnabled(true);
itemOne->setText(0, "John");
itemTwo->setText(0, "Peter");
itemThree->setText(0, "Kumar");
}
QMimeData *MainWindow
::mimeData(const QList<QTreeWidgetItem
*> items
) const {
// Create a QByteArray to store the data for the drag.
// Create a data stream that operates on the binary data
// Add each item's text for col 0 to the stream
for (int i=0;i<items.size();i++)
ds << items.at(i)->text(0);
// Set the data associated with the mime type foo/bar to ba
md->setData("foo/bar", text);
return md;
}
{
if (parent) {
// Create a QByteArray from the mimedata associated with foo/bar
// Create a data stream that operates on the binary data
while (!ds.atEnd()) {
// Read a byte from the stream into the string
ds >> str;
// Create a new item that has the item that is dropped on as a parent
newItem->setText(0, str);
}
}
return true;
}
{
e->accept();
}
{
e->accept();
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),QTreeWidget(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
ui->treeWidget->setColumnCount(1);
ui->treeWidget->setAcceptDrops( true );
// The tree supports dragging of its own items
ui->treeWidget->setDragEnabled(true);
QTreeWidgetItem *itemOne = new QTreeWidgetItem(ui->treeWidget);
QTreeWidgetItem *itemTwo = new QTreeWidgetItem(ui->treeWidget);
QTreeWidgetItem *itemThree = new QTreeWidgetItem(ui->treeWidget);
itemOne->setText(0, "John");
itemTwo->setText(0, "Peter");
itemThree->setText(0, "Kumar");
}
QMimeData *MainWindow::mimeData(const QList<QTreeWidgetItem *> items) const
{
QMessageBox::information(0,"",QString("Drag and Drop"));
// Create a QByteArray to store the data for the drag.
QByteArray text;
// Create a data stream that operates on the binary data
QDataStream ds(&text, QIODevice::WriteOnly);
// Add each item's text for col 0 to the stream
for (int i=0;i<items.size();i++)
ds << items.at(i)->text(0);
QMimeData *md = new QMimeData;
// Set the data associated with the mime type foo/bar to ba
md->setData("foo/bar", text);
return md;
}
bool MainWindow::dropMimeData(QTreeWidgetItem *parent, int index, const
QMimeData *data, Qt::DropAction action)
{
if (parent) {
// Create a QByteArray from the mimedata associated with foo/bar
QByteArray text = data->data("foo/bar");
// Create a data stream that operates on the binary data
QDataStream ds(&text, QIODevice::ReadOnly);
while (!ds.atEnd()) {
QString str;
// Read a byte from the stream into the string
ds >> str;
// Create a new item that has the item that is dropped on as a parent
QTreeWidgetItem *newItem = new QTreeWidgetItem(parent);
newItem->setText(0, str);
}
}
return true;
}
void MainWindow::dragEnterEvent(QDragEnterEvent *e)
{
e->accept();
}
void MainWindow::dragMoveEvent(QDragMoveEvent *e)
{
e->accept();
}
MainWindow::~MainWindow()
{
delete ui;
}
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
Bookmarks