首页 > 解决方案 > How can I add Icons to a QListWidget

问题描述

I have a program that has you drag and drop files into a QListWidget box and then you click a button to upload those files to a bucket. I would like to say which files have been uploaded and which one is currently being uploaded with an icon. Is there a way to add Icons inside/next to the QListWidget box?

Here is some of the code for the QListWidget and the drag and drop feature. I am just hoping there is a way to add icons

import sys, os
from PyQt5.QtWidgets import QApplication, QMainWindow, QListWidget, QListWidgetItem, QPushButton
from PyQt5.QtCore import Qt, QUrl
 
class ListBoxWidget(QListWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setAcceptDrops(True)
        self.resize(600, 600)
 
    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            event.accept()
        else:
            event.ignore()
 
    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(Qt.CopyAction)
            event.accept()
        else:
            event.ignore()
 
    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(Qt.CopyAction)
            event.accept()
 
            links = []
            for url in event.mimeData().urls():
                # https://doc.qt.io/qt-5/qurl.html
                if url.isLocalFile():
                    links.append(str(url.toLocalFile()))
                else:
                    links.append(str(url.toString()))
            self.addItems(links)
        else:
            event.ignore()
 
class AppDemo(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(1200, 600)
 
        self.listbox_view = ListBoxWidget(self)
 
        self.btn = QPushButton('Get Value', self)
        self.btn.setGeometry(850, 400, 200, 50)
        self.btn.clicked.connect(lambda: print(self.getSelectedItem()))
 
    def getSelectedItem(self):
        item = QListWidgetItem(self.listbox_view.currentItem())
        return item.text()
 
 
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
 
    demo = AppDemo()
    demo.show()
 
    sys.exit(app.exec_())

I have tried adding QIcon to the self.addItems(links) line but it continues to give me an error about arguments.

标签: pyqt5iconsqlistwidget

解决方案


Instead of adding items using addItems, create indivual QListWidgetItems and add them one by one using addItem(QListWidgetItem).

def dropEvent(self, event):
    if event.mimeData().hasUrls():
        event.setDropAction(Qt.CopyAction)
        event.accept()

        for url in event.mimeData().urls():
            # https://doc.qt.io/qt-5/qurl.html
            if url.isLocalFile():
                address = str(url.toLocalFile())
                icon = QIcon('localIcon.png')
            else:
                address = str(url.toString())
                icon = QIcon('remoteIcon.png')
            self.addItem(QListWidgetItem(icon, address))

If you want to change the icon of an existing item, access it using item() and use setIcon():

def setIconForItem(self, row, icon):
    self.listbox_view.item(row).setIcon(icon)
    

推荐阅读