zl2k
8th September 2008, 20:45
hi, there
I need to drag_select a group of icons in the qlistwidget and then drag_drop to another widget. Just by setting the properties of the qlistwidget, I can almost do that: I can drag_select and then drag on to another widget but can't drop. Then I did some search on the net and add a MyListWgt class so that the qlistwidget can promote to MyListWgt. After I did that, I can drag and drop but no longer can drag_select (I still can do multiple selections by click items individually). Please help.
Here is the code of the MyListWgt
#include "mylistwgt.h"
MyListWgt::MyListWgt(QWidget *parent): QListWidget(parent)
{
}
MyListWgt::~MyListWgt()
{
}
QStringList MyListWgt::mimeTypes () const
{
QStringList qstrList;
qstrList.append("text/uri-list");
return qstrList;
}
void MyListWgt::mouseMoveEvent(QMouseEvent *event)
{
if (!(event->buttons() & Qt::LeftButton)) return;
// if no item selected, return (else it would crash)
if (currentItem() == NULL) return;
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
// construct list of QUrls
// other widgets accept this mime type, we can drop to them
QList<QUrl> list;
list.append(QUrl(currentItem()->text())); // only QUrl in list will be text of actual item
// mime stuff
mimeData->setUrls(list);
drag->setMimeData(mimeData);
// start drag
drag->start(Qt::CopyAction | Qt::MoveAction);
}
Qt::DropActions MyListWgt::supportedDropActions () const
{
// returns what actions are supported when dropping
return Qt::CopyAction | Qt::MoveAction;
}
Here is the header file
#ifndef MYLISTWGT_H
#define MYLISTWGT_H
#include <QtGui>
#include <QListWidget>
#include <QStringList>
#include <fstream>
#include <iomanip>
#include <iostream>
/**
@author
*/
using namespace std;
class MyListWgt : public QListWidget{
Q_OBJECT;
public:
MyListWgt(QWidget *parent = 0);
~MyListWgt();
QStringList mimeTypes() const;
Qt::DropActions supportedDropActions () const;
void mouseMoveEvent(QMouseEvent *event);
};
#endif
Thanks for your comments.
zl2k
I need to drag_select a group of icons in the qlistwidget and then drag_drop to another widget. Just by setting the properties of the qlistwidget, I can almost do that: I can drag_select and then drag on to another widget but can't drop. Then I did some search on the net and add a MyListWgt class so that the qlistwidget can promote to MyListWgt. After I did that, I can drag and drop but no longer can drag_select (I still can do multiple selections by click items individually). Please help.
Here is the code of the MyListWgt
#include "mylistwgt.h"
MyListWgt::MyListWgt(QWidget *parent): QListWidget(parent)
{
}
MyListWgt::~MyListWgt()
{
}
QStringList MyListWgt::mimeTypes () const
{
QStringList qstrList;
qstrList.append("text/uri-list");
return qstrList;
}
void MyListWgt::mouseMoveEvent(QMouseEvent *event)
{
if (!(event->buttons() & Qt::LeftButton)) return;
// if no item selected, return (else it would crash)
if (currentItem() == NULL) return;
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
// construct list of QUrls
// other widgets accept this mime type, we can drop to them
QList<QUrl> list;
list.append(QUrl(currentItem()->text())); // only QUrl in list will be text of actual item
// mime stuff
mimeData->setUrls(list);
drag->setMimeData(mimeData);
// start drag
drag->start(Qt::CopyAction | Qt::MoveAction);
}
Qt::DropActions MyListWgt::supportedDropActions () const
{
// returns what actions are supported when dropping
return Qt::CopyAction | Qt::MoveAction;
}
Here is the header file
#ifndef MYLISTWGT_H
#define MYLISTWGT_H
#include <QtGui>
#include <QListWidget>
#include <QStringList>
#include <fstream>
#include <iomanip>
#include <iostream>
/**
@author
*/
using namespace std;
class MyListWgt : public QListWidget{
Q_OBJECT;
public:
MyListWgt(QWidget *parent = 0);
~MyListWgt();
QStringList mimeTypes() const;
Qt::DropActions supportedDropActions () const;
void mouseMoveEvent(QMouseEvent *event);
};
#endif
Thanks for your comments.
zl2k