[Solved]Drag and drop images between QListWidget and QGraphicsView
-
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?...
-
Then clean your mime data code and try something like this
@
QFileInfo info(item->text());
QString absoluteFilePath= info.absoluteFilePath();
QMimeData *mimeData = new QMimeData;
QList<QUrl> urlList;
urlList << QUrl::fromLocalPath(absoluteFilePath);
mimeData->setUrls(urlList);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
@DON'T just copy/paste be sure to analyze what it does (disclaimer: I didn't build nor run it, it's from memory)
-
Hi,
Thank you so much for a guidance, i understood now how the things works. Thanks for the knowledge sharing :)
But i have a strange error while executing
@
2013-08-30 17:20:47.708 Mainwindow[1760:707] __CFPasteboardIssueSandboxExtensionForPath: error for [/Users/venkateshpadmanabhan/Desktop/build-Mainwindow-Desktop-Debug/Mainwindow.app/Contents/MacOS/IMG_2452.JPG]
@
I don't even get a clue what does it mean, i tried to google it but in vain...
-
What version of Qt and OS X are you running ?
-
Hi,
Thanks for your reply. I found the error. It was in populating the datas onto my QListWidget. My code is
@
void Widget::on_pushButton_clicked()
{
// QStringList fileName = QFileDialog::getOpenFileName(this,
// tr("Open File"), QDir::currentPath());
//ui->listwidget->addItems(fileName);QString fileName; QStringList fileNames; QFileDialog dialog(this); dialog.setFileMode(QFileDialog::ExistingFiles); if (dialog.exec()) fileNames = dialog.selectedFiles(); foreach (fileName, fileNames) { QListWidgetItem *item = new QListWidgetItem(ui->listwidget); ui->listwidget->setViewMode(QListWidget::IconMode); QFileInfo fi(fileName); QString name = fi.absoluteFilePath(); item->setText(name); QIcon icon; icon.addPixmap(fileName); item->setIcon(icon); item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled); ui->listwidget->setIconSize(QSize(100,100)); ui->listwidget->update(); }
// ui->listwidget->update();
}
@
If i use @fi.fileName@ the drag and drop is not working as we are using only the text of the QListWidgetItem to get the full path. The problem is now when is use @fi.absolutefilePath@ The drag and drop works pretty fine. But in my QListWidget, whole path of the image is displayed as text, which doesnt look pretty. Love to have some suggestion how to tackle the problem. -
You can use the QListWidgetItem's data/setData functions storing the path using Qt::UserRole and set the text as you did before.
-
Hi,
I tried during populating the datas into QListWidget
@
setData(Qt::UserRole, name);
//item->setText(name);
@To hide the file name, showing only the thumbnails but the drag and drop doesnt work. I tried
@
data(Qt::UserRole);
@This too doesn't work. How should i use it to hide the data from the user in QListWidget. Awaiting your response.
-
Use setText(name) so you have your name in your item.
Use setData to store the path.data will return a QVariant. Look at the doc of QVariant to see how to retrieve the string from it.
-
Hi,
I used while populating into QlistWidget@
QVariant v(fileName);
item->setData(Qt::UserRole,v);
@and while retrieving data
@
QVariant v=data(Qt::UserRole);
QString s=v.toString();
@This works perfect thank you so much :)
-
Hi,
Please do suggest your guidance for my new post "here" :http://qt-project.org/forums/viewthread/32022/ . And i know it is a dumb question.I would like to how to add the title as [solved]. I tried to find the way to do it but couldn't edit the title. I could edit only my post -
You can do that by editing the first post
-
Hi,
Thanks I have done that, awaiting your guidance for the new post :) it's been great privilege to have your wonderful support throughout my learning curve...