#include <QApplication>
#include <QListWidget>
#include <QMimeData>
#include <QMessageBox>
#include <QtDebug>
public:
setDragEnabled(true);
setAcceptDrops(true);
setDragDropMode(DragDrop);
}
protected:
}
QMimeData * mimeData
( const QList<QListWidgetItem
*> items
) const{ qDebug
("MD: %d", items.
count());
QMimeData *md
= new md
->setData
("application/x-internal",
QString::number(r
).
toAscii());
return md;
}
bool dropMimeData
( int index,
const QMimeData * data, Qt
::DropAction action
){ qDebug("DMD");
QByteArray ba
= data
->data
("application/x-internal");
int r = rstr.toInt();
// move from r to index
QMessageBox::information(this,
"DRAG&DROP",
QString("Taken from %1 and dropped to %2").
arg(r
).
arg(index
));
if(r==index)
return false;
if(r<index){
insertItem(index-1, item);
} else {
insertItem(index, item);
}
return true;
}
};
int main(int argc, char **argv){
LW lw;
for(int i=0; i<10; i++){
}
lw.show();
return app.exec();
}
#include <QApplication>
#include <QListWidget>
#include <QMimeData>
#include <QMessageBox>
#include <QtDebug>
class LW : public QListWidget {
public:
LW() : QListWidget(){
setDragEnabled(true);
setAcceptDrops(true);
setDragDropMode(DragDrop);
}
protected:
QStringList mimeTypes () const{
return QStringList() << "application/x-internal";
}
QMimeData * mimeData ( const QList<QListWidgetItem *> items ) const{
qDebug("MD: %d", items.count()); QMimeData *md = new
QMimeData; int r = row(items.at(0));
md->setData("application/x-internal", QString::number(r).toAscii());
return md;
}
bool dropMimeData( int index, const QMimeData * data, Qt::DropAction action ){
qDebug("DMD");
QByteArray ba = data->data("application/x-internal");
QString rstr(ba);
int r = rstr.toInt();
// move from r to index
QMessageBox::information(this, "DRAG&DROP", QString("Taken from %1 and dropped to %2").arg(r).arg(index));
if(r==index)
return false;
QListWidgetItem *item = takeItem(r);
if(r<index){
insertItem(index-1, item);
} else {
insertItem(index, item);
}
return true;
}
};
int main(int argc, char **argv){
QApplication app(argc, argv);
LW lw;
for(int i=0; i<10; i++){
new QListWidgetItem(QString("ITEM %1").arg(i+1), &lw);
}
lw.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks