[Solved]Drag and drop images between QListWidget and QGraphicsView
-
Then, how can i use the url without initialising?...
-
Please, read "QMimeData's documentation":http://qt-project.org/doc/qt-4.8/qmimedata.html#details It's shown there.
-
else if (event->mimeData()->hasUrls())
Hi,I read the documentation and tried this. But no result. I have tried to add the urls in the mimeData into a QString to get the path into it and passed the QString to pixmap to add it to the scene. But nothing happens...
@
{
QList<QUrl> urlList = mimeData->urls();
QString text;
for (int i = 0; i < urlList.size() && i < 32; ++i) {
QString url = urlList.at(i).path();
text += url + QString("\n");
}
QPixmap pix(text);
scene()->addPixmap(pix);
update();
event->acceptProposedAction();
//}
}
}@
Where am i going wrong ???...
-
You are appending each url to text with an additional carriage return so text will not contain a valid path.
-
Hi, now i am able to drag and drop urls into the qgraphicsview
code is
@
// } QList<QUrl> urls = event->mimeData()->urls(); if (urls.isEmpty()) return; QString fileName = urls.first().toLocalFile(); QPixmap pix(fileName); scene()->addPixmap(pix); update(); event->acceptProposedAction();
@
:)
-
So problem solved ?
Don't forget to update the thread's title then :)
-
Hi,
But my real application has to accept drags from the QListWidget. I have implemented it and promoted the QlistWidget to the class but QgraphicsView doesnt accept the drag.
My Code:
@
listWidget::listWidget(QWidget *parent)
: QListWidget(parent)
{
setDragEnabled(true);
setViewMode(QListView::IconMode);
setDragDropMode(QAbstractItemView::DragDrop);
setSelectionMode(QAbstractItemView::SingleSelection);}
void listWidget::dragLeaveEvent(QDragLeaveEvent event)
{
event->accept();
}
void listWidget::mouseMoveEvent(QMouseEvent event)
{
// if (!(event->buttons() & Qt::LeftButton)) return;
//if (currentItem() == NULL) return;
QDrag drag = new QDrag( this );
QMimeData mimeData = new QMimeData;
QList<QUrl>list;
list.append(QUrl(currentItem()->text()));
mimeData->setUrls(list);
drag->setMimeData(mimeData);
drag->start(Qt::CopyAction | Qt::MoveAction);
event->setAccepted(true);
}void listWidget::performDrag()
{
QListWidgetItem *item = currentItem();
if (item) {
QMimeData *mimeData = new QMimeData;
QList<QUrl>list;
mimeData->setUrls(list);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
emit itemDrag();
if (drag->exec(Qt::MoveAction | Qt::CopyAction))
// delete item;
}
}void listWidget::dragEnterEvent(QDragEnterEvent *event)
{event->acceptProposedAction();
}
void listWidget::dragMoveEvent(QDragMoveEvent *event)
{
event->acceptProposedAction();
}@
Whats the problem with it?. As the QGraphicsView is already modelled to accept the URL. I think i should only have to implement the QListWidget to accept Drag right? and add mimeData to the drag. I am confused where i am going wrong...
-
Are you sure your view accepts drops properly ?
-
Hi,
Yes my view accepts drops of url from outside the application but it doesnt accept from the listWidget... I dont know why.
-
Then you should subclass QGraphicsView and implement dropEvent to test that (don't forget to call the base implementation otherwise it'll break the current functionality)
-
Hi,
I get the following error during the drag and drop from my QListWidget. I couldnt understand what it means
@
2013-08-29 12:28:18.743 Mainwindow[990:707] __CFPasteboardIssueSandboxExtensionForPath: error for [/IMG_2458.JPG]
@
-
Hi,
As i have already created
@
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
@for accepting drops from outside the application into QGraphicsView, it works fine. But it doesn't work when i drop from QListWidget in the same application. Should i again recreate these for accepting drops from QListWidget?
-
Does the url in your dragged item contain a complete path ?
-
Hi,
The problem is i am not sure how to get the path of the qlistwidget item. I am trying to find it but i couldn't. It would be great if you could guide me how to get the path of the current item.This is how i have implemented
@#include "listWidget.h"
#include "ui_widget.h"
#include "widget.h"
#include <QtGui>listWidget::listWidget(QWidget *parent)
: QListWidget(parent)
{
setDragEnabled(true);
setViewMode(QListView::IconMode);
//setDragDropMode(QAbstractItemView::DragDrop);
setSelectionMode(QAbstractItemView::SingleSelection);
}
void listWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
startPos = event->pos();
QListWidget::mousePressEvent(event);}
void listWidget::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
int distance = (event->pos() - startPos).manhattanLength();
if (distance >= QApplication::startDragDistance())
startDrag();
}
QListWidget::mouseMoveEvent(event);
}
void listWidget::startDrag()
{
QListWidgetItem *item =currentItem();
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);if (item) { QFileInfo info(item->text()); QString str= info.absoluteFilePath(); QByteArray itemData; QDataStream dataStream(&itemData, QIODevice::WriteOnly); QPixmap pixmap = qVariantValue<QPixmap>(item->data(Qt::UserRole)); QPoint location = item->data(Qt::UserRole+1).toPoint(); dataStream <<pixmap<<location; QMimeData *mimeData = new QMimeData; mimeData->setData(str,itemData); QDrag *drag = new QDrag(this); drag->setMimeData(mimeData); drag->setPixmap(pixmap); //if (drag->start(Qt::MoveAction) == Qt::MoveAction) // delete item;
}
}
@
-
AFAICS you don't set any url in your mime data
-
Hi,
@
QString str@
contains the full path i just tested it by adding the string to the mime data and dropped it into desktop to check. I could get a text file containing full path. But i dont know why the above code doesnt work... Should i also implement something at the QGraphicsView to accept the drop? Though i have already implemented for receiving drops if there is an image, and url. It works perfectly for drops from outside the application. Awaiting your further guidance... -
You're using setData in a wrong manner. The string parameter should contain the mime type of the data you are passing as the second parameter.
-
Hi,
Is this the only way i could implement it?.. I am bit confused. The mimeType which i am using here is path of the data, so should it be QUrl??... It would be great to have your guidance how to set the mimeType for my application. I am trying to find it. But couldnt get a concrete idea about the implementation. Thanks for your valuable time and help.
-
Do you use the content of itemData in your drop function ?
-
hi,
No i didnt modify my drop function. I just wrote it only to accept Url and Images.... Should i modify my dropfunction in qgraphicsView implementation?... How should that be done?...