PDA

View Full Version : drag_drop blocks drag_select, please help



zl2k
8th September 2008, 21: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

wysota
9th September 2008, 21:42
Why did you reimplement the mouseMove event? It's enough to reimplement QListWidget::mimeData() to provide your own mime data for items in the list.

zl2k
9th September 2008, 23:26
Ok, I blocked the reimplement of mouseMove event and reimplement QListWidget::mimeData() as following:


QMimeData * MyListWgt::mimeData ( const QList<QListWidgetItem *> items ) {
QList<QUrl> list;
list.append(QUrl(currentItem()->text())); // only QUrl in list will be text of actual item

// mime stuff
QMimeData *mimeData = new QMimeData;
mimeData->setUrls(list);
return mimeData;
}

It did not solve the problem. The icons in the listwidget are not consistent after drap&drop (sometimes missing the caption with only icons shown, sometimes missing both). What I want is just pass the selected item info into the drop window with the items in the drag windown unchanged (like copy, but don't really duplicate the items).

wysota
9th September 2008, 23:28
Did you reimplement dropMimeData() as well?