[Qt-interest] Dropmimedata()

Yuvaraj R yuvaraj at ongobiz.com
Sat Jul 4 15:35:31 CEST 2009


Hi All

QtreeWidget Drag and Drop , dropminedata function is not called,  though i
defined..

Sample code..

 * mainwindow.h*
mainwindow.cpp

#ifndef MAINWINDOW_H#define MAINWINDOW_H
 #include <QtGui/QMainWindow>#include <QTreeWidgetItem>#include <QMimeData>
 namespace Ui{
    class MainWindowClass;}

class MainWindow : public QMainWindow
<http://doc.trolltech.com/latest/QMainWindow.html>{
    Q_OBJECT
 public:
    MainWindow(QWidget <http://doc.trolltech.com/latest/QWidget.html>
*parent = 0);
    ~MainWindow();
     bool dropMimeData(QTreeWidgetItem
<http://doc.trolltech.com/latest/QTreeWidgetItem.html> *parent, int
index, const QMimeData
<http://doc.trolltech.com/latest/QMimeData.html> *data, Qt::DropAction
action);
     QMimeData <http://doc.trolltech.com/latest/QMimeData.html>
*mimeData(const QList<QTreeWidgetItem *> items) const;private:
    Ui::MainWindowClass *ui;protected:
       void dragEnterEvent(QDragEnterEvent
<http://doc.trolltech.com/latest/QDragEnterEvent.html> e);
        void dragMoveEvent(QDragMoveEvent
<http://doc.trolltech.com/latest/QDragMoveEvent.html> e) ;

 };
 #endif // MAINWINDOW_H

*mainwindow.cpp*

  #include "mainwindow.h"#include "ui_mainwindow.h"#include <QMessageBox>
MainWindow::MainWindow(QWidget
<http://doc.trolltech.com/latest/QWidget.html> *parent)
    : QMainWindow
<http://doc.trolltech.com/latest/QMainWindow.html>(parent), ui(new
Ui::MainWindowClass){
    ui->setupUi(this);
    ui->treeWidget->setColumnCount(2);
    ui->treeWidget->setAcceptDrops( true );
        // The tree supports dragging of its own items
     ui->treeWidget->setDragEnabled(true);
      QTreeWidgetItem
<http://doc.trolltech.com/latest/QTreeWidgetItem.html> *itemOne = new
QTreeWidgetItem
<http://doc.trolltech.com/latest/QTreeWidgetItem.html>(ui->treeWidget);
        QTreeWidgetItem
<http://doc.trolltech.com/latest/QTreeWidgetItem.html> *itemTwo = new
QTreeWidgetItem
<http://doc.trolltech.com/latest/QTreeWidgetItem.html>(ui->treeWidget);
        QTreeWidgetItem
<http://doc.trolltech.com/latest/QTreeWidgetItem.html> *itemThree =
new QTreeWidgetItem
<http://doc.trolltech.com/latest/QTreeWidgetItem.html>(ui->treeWidget);

        itemOne->setText(0, "John");
        itemTwo->setText(0, "Peter");
        itemThree->setText(0, "Kumar");}

MainWindow::~MainWindow(){
    delete ui;}QMimeData
<http://doc.trolltech.com/latest/QMimeData.html>
*MainWindow::mimeData(const QList<QTreeWidgetItem *> items) const{
        // Create a QByteArray to store the data for the drag.
        QByteArray <http://doc.trolltech.com/latest/QByteArray.html> text;
        // Create a data stream that operates on the binary data
        QDataStream <http://doc.trolltech.com/latest/QDataStream.html>
ds(&text, QIODevice
<http://doc.trolltech.com/latest/QIODevice.html>::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 <http://doc.trolltech.com/latest/QMimeData.html> *md
= new QMimeData <http://doc.trolltech.com/latest/QMimeData.html>;
        // Set the data associated with the mime type foo/bar to ba
        md->setData("foo/bar", text);
        return md;}bool MainWindow::dropMimeData(QTreeWidgetItem
<http://doc.trolltech.com/latest/QTreeWidgetItem.html> *parent, int
index, const
                                             QMimeData
<http://doc.trolltech.com/latest/QMimeData.html> *data, Qt::DropAction
action){
        if (parent) {
               // Create a QByteArray from the mimedata associated with foo/bar
               QByteArray
<http://doc.trolltech.com/latest/QByteArray.html> text =
data->data("foo/bar");
               // Create a data stream that operates on the binary data
               QDataStream
<http://doc.trolltech.com/latest/QDataStream.html> ds(&text, QIODevice
<http://doc.trolltech.com/latest/QIODevice.html>::ReadOnly);
               while (!ds.atEnd()) {
                      QString
<http://doc.trolltech.com/latest/QString.html> 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
<http://doc.trolltech.com/latest/QTreeWidgetItem.html> *newItem = new
QTreeWidgetItem
<http://doc.trolltech.com/latest/QTreeWidgetItem.html>(parent);
                      newItem->setText(0, str);
               }
        }
        return true;}void MainWindow::dragEnterEvent(QDragEnterEvent
<http://doc.trolltech.com/latest/QDragEnterEvent.html> event){
    event.accept();}


  void MainWindow::dragMoveEvent(QDragMoveEvent
<http://doc.trolltech.com/latest/QDragMoveEvent.html> event)
  {
   event.accept();
  }

what 's problem with above code..


Thanks

Yuvaraj R







On Fri, Jul 3, 2009 at 10:58 PM, Yuvaraj R <yuvaraj at ongobiz.com> wrote:

>
> please can any help me sort out this issue
>
> Thanks
>
> Yuvaraj R
>
>
> On Fri, Jul 3, 2009 at 10:02 PM, Yuvaraj R <yuvaraj at ongobiz.com> wrote:
>
>> Hi all
>>
>>
>> Please parden me ..if  my issue is silly
>>
>> I am struggling with QTreeWidget  Drag and drop  issue since one week..
>>
>> i know answer is two line code only.. I don't know how to compare the Drop
>> Event with  top level items
>>
>> How i set the Drop Event function only for accepting top level items..
>>
>> please help me..
>>
>> Advance thanks for helping
>>
>>
>> Thanks
>>
>> Yuvaraj R
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090704/97c85fe1/attachment.html 


More information about the Qt-interest-old mailing list