[Qt-interest] Dropmimedata()

Yuvaraj R yuvaraj at ongobiz.com
Mon Jul 6 07:48:04 CEST 2009


Hi   over ride the bool mimedata and Dragenter events like

*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;

private:

Ui::MainWindowClass *ui;

protected:

void dragEnterEvent(QDragEnterEvent *e);

void dragMoveEvent(QDragMoveEvent *e) ;

};

#endif // MAINWINDOW_H



*mainwindow.cpp *
 #include "mainwindow.h"

#include "ui_mainwindow.h"

#include <QMessageBox>

#include <Qdebug>

MainWindow::MainWindow(QWidget *parent)

: QMainWindow(parent),QTreeWidget(parent), ui(new Ui::MainWindowClass)

{

ui->setupUi(this);

qDebug() << "mainwondow function";

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

{

// 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)

{

QMessageBox::information(0,"",QString("Drag and Drop"));

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 *event)

{

qDebug() << "drag enter event";

event->accept();

}

 void MainWindow::dragMoveEvent(QDragMoveEvent *event)

{qDebug() << "drop enter event";

event->accept();

}

MainWindow::~MainWindow()

{

delete ui;

}



I have one doubt.. Here mainwindow  inheritance from two class
QMainwindow,QTreeWidget,which class Drag enter
Drag move  event ,it will take.


Thanks

Yuvaraj R



On Sat, Jul 4, 2009 at 7:45 PM, Yuvaraj R <yuvaraj at ongobiz.com> wrote:

> Hi
>    Thanks for your reply
>
> How  can i solve this issue..
>
> please help me
>
> Thanks
>
>  Yuvaraj R
>
>
> On Sat, Jul 4, 2009 at 7:38 PM, J-P Nurmi <jpnurmi at gmail.com> wrote:
>
>> On Sat, Jul 4, 2009 at 4:35 PM, Yuvaraj R<yuvaraj at ongobiz.com> wrote:
>> > Hi All
>> >
>> > QtreeWidget Drag and Drop , dropminedata function is not called,  though
>> i
>> > defined..
>> >
>> > ...
>> >
>> > class MainWindow : public QMainWindow
>> > {
>> > ...
>> >      bool dropMimeData(QTreeWidgetItem *parent, int index, const
>> QMimeData
>> > *data, Qt::DropAction action);
>> > ...
>> > };
>>
>> It's a virtual method of QTreeWidget, not QMainWindow. You are
>> supposed to inherit QTreeWidget and reimplement it in the QTreeWidget
>> subclass.
>>
>> --
>> J-P Nurmi
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090706/e6990bf1/attachment.html 


More information about the Qt-interest-old mailing list