PDA

View Full Version : QDropEvent



addu
3rd July 2009, 18:02
Hi All

i am facing the same QTreeWidget Drag and Drop issue since last 1.5 weeks..

I am feeling this is very bad me..

My problem is How can i set top level items only has to accepts drops.

I set following things

ui->widget->dragDropOverwriteMode();
ui->treeWidget->setDragEnabled(true);
ui->treeWidget->setAcceptDrops(true);
ui->treeWidget->setDragDropMode(QAbstractItemView ::InternalMove);


I know answer is very simple, But i don't know how do compare the Drop Event with top level items ...

please help me to sort out this issue..

Any suggestions welcome.

Thanks

Addu R

wysota
3rd July 2009, 20:19
Use QTreeWidgetItem::setFlags().

addu
4th July 2009, 05:28
Dear wysota

Thanks for your reply..

I enabled the Drop action only for top level items by

QTreeWidgetItem * friends = new QTreewidgetItem(ui->treeWidget);

freinds->setFlags(Qt::Items is Drop Enabled);

but same thing all items are accepting the drops...

And one more thing dafter dropping the any one item to friends ,it behaving like child item


Thanks

Addu R

wysota
4th July 2009, 07:55
Make sure InternalMove is disabled. If that doesn't help, please provide a minimal compilable example reproducing the problem.

addu
4th July 2009, 08:44
Previously i used the internalmove mode, Then i changed to DragDrop mode.. But it is not working for me ..

sample code



ui->treeWidget->dragDropOverwriteMode();
ui->treeWidget->setDragEnabled(true);
ui->treeWidget->setAcceptDrops(true);
ui->treeWidget->setDragDropMode(QAbstractItemView ::DragDrop);
QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
item->setText(0,"Friends");
QList <QString > list;
list << tr("john") << tr("joseph") << tr("stephen") << tr("raj") << tr("Addu") ;

for ( int i=0;i < list.size;i++)
{
QTreeWidgetItem *new_item = new QTreeWidgetItem(item);
new_item->setText(0,list.at(i));
}


Again i am creating new top level item for TreeWidget



QTreeWidgetItem *colleages = new QTreeWidgetItem(ui->treewidget);
colleages->setText(0,"co-Workers");



I am trying to drag the items from friends to co-workers .. Here top level item only accept the drops..child items won't accept the drops.


How do i sort out this issue


Thanks

Addu R

wysota
4th July 2009, 09:08
Ok, but how do you set the flags and how do you handle the drop itself?

addu
4th July 2009, 09:33
I set the flags only for top level items ..


item->setFlag(Qt::ItemisDropEnabled);
colleages->setFlag(Qt::ItemisDropEnabled);
new_item->setFlag(Qt::Itemis DragEnabled);

Previoused i over ride the DropEvent function..
But if i set the ui->treeWidget->setAcceptDrops(true),DropEvent function was not called ..

So I commented the DropEvent function..

And i did like this


void MainWindow::dropEvent(QDropEvent *event)
{
ui->treeWidget->setDropEvent(event) // Here Application is crashing

}

Where i have done mistake,please correct me, i know problem is at DropEvent function..

please help me to clear this issue

Thanks

Addu R

wysota
4th July 2009, 09:36
The mistake you did was that you didn't bother to look into the documentation.

QTreeWidget::dropMimeData()

addu
4th July 2009, 10:08
Dear Wysota..

Thanks for your valuable solution.

I implemented the following function



void MainWindow::dragEnterEvent(QDragEnterEvent *eve)
{
qDebug() << "Drag enter event";
eve->accept();

}
void MainWindow::dragMoveEvent(QDragMoveEvent *e)
{

qDebug() << "Drag move event";
e->accept();
}
bool MainWindow::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
{
QMessageBox::information(0,"",QString("Addu"));
}




but complier is not going to dropMimeData function

wysota
4th July 2009, 10:21
Because you still didn't bother to look into the docs. This time I will not give you a pointer but instead you're given a classic RTFM. I'll give you a hint though - you want to read about performing drag and drop with Item Views.

addu
4th July 2009, 10:33
Wysota

i have gone through the below link

http://www.qtsoftware.com/developer/faqs/faq.2008-05-23.4700087965..

same thing only i followed ...please i request you provide me one sample example


Thanks

Addu R

wysota
4th July 2009, 11:02
Learn, don't copy without thinking.

addu
4th July 2009, 11:44
I am trying to execute sample example.. while executing i am getting following errors

mainwindow.h



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QTreeWidgetItem>
#include <QMimeData>

namespace Ui
{
class MainWindowClass;
}

class MainWindow : public QMainWindow
{
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>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(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 *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");
}

MainWindow::~MainWindow()
{
delete ui;
}
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)
{
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)
{
event.accept();
}


void MainWindow::dragMoveEvent(QDragMoveEvent event)
{
event.accept();
}




errors:
error: `event' has incomplete type
error: forward declaration of `struct QDragEnterEvent'
error: invalid use of undefined type `struct QDragEnterEvent'
error: forward declaration of `struct QDragEnterEvent'
error: `event' has incomplete type

wysota
4th July 2009, 12:01
You didn't include proper files so the compiler complains about it.

addu
4th July 2009, 13:37
Hi wysota

In above example .. when running the application mimedata, Dropmimedata is never invoked..

I put the QmessageBox and qDebug() statement there.. But i didn't get any responce from that functions..


Thanks

Yuvaraj R

wysota
4th July 2009, 13:41
It will never be invoked because you're overriding the whole drag&drop mechanism present in Item Views. And you still didn't bother to read the docs...

addu
4th July 2009, 13:57
Hi wysota..

I have been reading the doc and right now i am referring the item/view based Puzzle example...

whenever make drag the data are stored at time mime data function will be called ..

whenver we dropping the item that time dropmime data function will be called..

if i don't override drag and drop functions also that two functions are not invoking...



Thanks

Addu R

wysota
4th July 2009, 16:38
You only need to override mimeData() and dropMimeData(), nothing more.

addu
6th July 2009, 06:41
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;
}

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


mainwindow.cpp

#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;
}




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

nish
6th July 2009, 07:12
hey man.. u cannot inherit from both QMainWindow and QTreeWidget. make seperate classes for both of them.. ..
tomoro if i get the Fkin time i will read your posts and problems..

wysota
6th July 2009, 07:23
class MainWindow : public QMainWindow,public QTreeWidget

Ah man.... did you even try to compile this code?

At moments like this I'm beginning to think if we're even remotely close to managing to educate people here...

Maybe we should get some programming certification scheme here or something...

nish
6th July 2009, 07:31
what i found is many ppl know c++ but do not know one of these concepts 1. inheritance 2. virtual 3. pointers. And they jump into Qt. This where all starts to mess up.

wysota
6th July 2009, 07:54
what i found is many ppl know c++ but do not know one of these concepts 1. inheritance 2. virtual 3. pointers.

It's like "they know how to drive but only on the first gear and only straight ahead". It's hard to call it "knowing how to drive" then... But that you don't know how to program is not a big deal. It starts to be a deal when you're not learning and still continue to bang your head against the wall.

faldzip
6th July 2009, 08:29
...but do not know one of these concepts...
and sometimes not only one :P

nish
6th July 2009, 08:51
i have seen ppl make very good/fast newbiew programs in c++ ... but they seem to take ages to get into inheritance thing...

wysota
6th July 2009, 09:08
i have seen ppl make very good/fast newbiew programs in c++
So was it c++ or c? :)

Ok, we're getting offtopic here.

nish
6th July 2009, 09:25
So was it c++ or c? :)
they used cout<< and cin>> .. so i think it was c++;)


Ok, we're getting offtopic here.
ya.. just like to share one incident (which i remembered by reading this thread) if you permit..:)...
once i was taking interview of a candidate who had 5+ years of programming exp in Qt. I asked, "If i inherit QPushButton and QCombobox in my class, how would the widget will look like?".

He replied: "If You inherit QPushButton first (like class w : public QPushbutton, QCombobox) then the widget will have the appearance of a button and vice versa".
REJECTED.

I thought then that he was a masterpiece and i will never find someone like him again. Guess i was wrong:)