PDA

View Full Version : Sperimenting with drag&drop



jiveaxe
28th July 2011, 11:06
Hi, as in the subject I started playing with drag and drop. The idea is to have a QListWidget filled with some items then dragging one item on the desktop (i'm using kde) it should create a plasmoid with the item's text. I prepared the plasmoid and added to .desktop the line X-Plasma-DropMimeTypes=text/plain. Next I coded the app: have created a new class subclassing QListWidget and redefined QStringList ListWidget::mimeTypes () to return "plain/text" as mime type.

Now, after compiling it, if i try to drag an item on the desktop correctly popupS a menu asking which plasmoid create (between mine and notes witdget) but, no matter what I choose, I got an empty plasmoid: no text is passed dragging.

Here the code:

header


#include <QtGui/QMainWindow>
#include <QtGui/QListWidget>

class ListWidget : public QListWidget
{
Q_OBJECT
public:
ListWidget(QWidget *parent);

protected:
QStringList mimeTypes () const;
};

class PassMime : public QMainWindow
{
Q_OBJECT
public:
PassMime();
virtual ~PassMime();
QStringList i18n(const char* arg1);
};


implementation


#include <QtGui/QLabel>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QAction>
#include <QtGui/QListWidget>
#include <QtCore/QMimeData>
#include <QtCore/QDebug>

ListWidget::ListWidget(QWidget* parent): QListWidget(parent)
{

}

QStringList ListWidget::mimeTypes () const
{
QStringList list;
list << "text/plain";

return list;
}

PassMime::PassMime()
{
QStringList sl;
sl << "apple";
sl << "orange";
sl << "lemon";

ListWidget *l = new ListWidget(this);
l->setDragEnabled(true);
foreach(QString s, sl) {
QListWidgetItem *item = new QListWidgetItem;
item->setText(s);
l->addItem(item);
}

setCentralWidget( l );
QAction* a = new QAction(this);
a->setText( "Quit" );
connect(a, SIGNAL(triggered()), SLOT(close()) );
menuBar()->addMenu( "File" )->addAction( a );
}

PassMime::~PassMime()
{}


I tried to override QMimeData * QListWidget::mimeData(const QList<QListWidgetItem *> items) with this new one:


QMimeData * ListWidget::mimeData(const QList<QListWidgetItem *> items)
{
QString text;
foreach(QListWidgetItem *i, items) {
text += i->text() + "\n";
}

QMimeData *data = new QMimeData;
data->setText(text);

return data;
}

but probably I did it the wrong way since still doesn't work.

What I miss?

Cheers