PDA

View Full Version : Drag from QListWidget



Pradeep V
31st August 2015, 11:45
Hi All,

I am trying to Drag from a QListWidget and drop it into QTextBrower/ QLineEdit.

I am trying to copy "Text/plain" to the mimedata by doing this:



void MainWindow::dragEnterEvent(QDragEnterEvent *Event)
{
QString my_text;
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
my_text = Event->mimeData()->text();
mimeData->setText(my_text);
drag->setMimeData(mimeData);
drag->exec();
}


But in the dropEvent function, when i capture the mimedata, it is of "application/x-qabstractitemmodeldatalist" format.



void MainWindow::dropEvent(QDropEvent *Event)
{
QString my_text;
QStringList myformats = Event->mimeData()->formats();
foreach(QString format, myformats)
qDebug() << format;

qDebug() << my_text;
Event->acceptProposedAction();
}



Kindly help me in dropping the mimedata to QLineEdit or QTextBrowser.

Thanks

anda_skoa
31st August 2015, 15:59
void MainWindow::dragEnterEvent(QDragEnterEvent *Event)
{
QString my_text;
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
my_text = Event->mimeData()->text();
mimeData->setText(my_text);
drag->setMimeData(mimeData);
drag->exec();
}


Why do you want to start a drag when a drag enters the widget?
Wouldn't it be better to handle the incoming drag?



But in the dropEvent function, when i capture the mimedata, it is of "application/x-qabstractitemmodeldatalist" format.

That's what a QAbstractItemView based view class will use.

Cheers,
_

Pradeep V
1st September 2015, 04:39
I am very much new to Qt.. Can you elaborate more so that i can understand better.

anda_skoa
1st September 2015, 08:41
I am very much new to Qt.. Can you elaborate more so that i can understand better.

The default implementation of QAbstractItemView::startDrag() calls the model's mimeData() method to fill the drag object.
The default implementation of that uses the MIME type you see and encodes the data such that it or another instance of it can decode it again.

You can look at the model's code, or implement your own mimeData() or implement your own startDrag() to encode the data differently.
Ah, you wrote QListWidget: in this case you can overwrite its mimeTypes() an mimeData() methods to encode the data the way you want.

Cheers,
_