[Solved]Drag and drop images between QListWidget and QGraphicsView
-
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...