Solved getting the icon of a dropped file
-
I am using qt5 and I have a qlistwidget that accepts drops. The drops work as I expect, now I would like to have the file or folder icon next to the path in the qlistwidgetitem. I see that it does accept an icon, I need to know how to get the icons. From looking through the autocomplete it looks like it is only a list of urls. What can I do to get the icons for the files?
-
-
ok I found out how to get the icon, but I was under the impression that giving an icon in the constructor of a new qlistwidgetitem would make it display the icon next to the text? Is there some special way to make the icon appear?
here is the relevant code
void myQListWidget::dropEvent(QDropEvent *event)
{
qDebug() << "dropEvent";if (event->mimeData()->hasUrls()) { QList<QUrl> urls = event->mimeData()->urls(); QUrl url; foreach (url,urls) { QIcon icon(url.toLocalFile()); this->addItem(new QListWidgetItem( icon, url.toLocalFile())); } event->acceptProposedAction(); } QListWidget::dropEvent(event);
}
-
Did you check that the Icon is valid ?
-
ya I was doing it wrong. Thanks I got it working, here is the code now
void myQListWidget::dropEvent(QDropEvent *event) { qDebug() << "dropEvent"; if (event->mimeData()->hasUrls()) { QList<QUrl> urls = event->mimeData()->urls(); QUrl url; foreach (url,urls) { QFileInfo file(url.toLocalFile()); QFileIconProvider iconProvider; QIcon icon(iconProvider.icon(file)); this->addItem(new QListWidgetItem( icon, url.toLocalFile())); } event->acceptProposedAction(); } QListWidget::dropEvent(event); }