如果你想在你的 QListWidget 中放置带有文本的图像,你需要将 QIcon 与 Text 组合成一个 QListWidgetItem ,如下所示:
from PyQt5 import QtGui, QtCore, QtWidgets
import os
icon_path = os.getcwd() + "\icon.jpg"
titles = ['title1','title2']
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
#add list of strings
self.listWidget.addItems(titles)
#create QIcon
icon = QtGui.QIcon(icon_path)
#Create the row with Icon + Text
item = QtWidgets.QListWidgetItem(icon, "Hello this is a test")
#set size of the item
size = QtCore.QSize()
size.setHeight(100)
size.setWidth(400)
item.setSizeHint(size)
#Put the item into the widget
self.listWidget.addItem(item)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
app.exec_()
你会得到这样的东西: