Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating list with images in PYQT

I have an external database with images of products. Is it possible to import these images and display them in a scrolling list, as well as making them user clickable, similar to how a file browser works?

I can only find information on people converting to resource files, but I wonder if its possible to skip that?

like image 268
user-2147482637 Avatar asked Apr 04 '14 06:04

user-2147482637


1 Answers

Ok ListView and ListWidget both allow users to display content in either list Mode or Icon Mode. So you can set the view mode to icon mode and display image in the list View.

self.listView.setViewMode(QtGui.QListView.IconMode)

or

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

put the above code outside your class.

self.listWidget.setViewMode(QtGui.QListView.IconMode)
item = QtGui.QListWidgetItem()
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/sameold/capture_14.jpg")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
item.setIcon(icon)
self.listWidget.addItem(item)

Here is the output for this .

enter image description here


import os
files=[]
for file in os.listdir("C:/"):
    if file.endswith(".jpeg"):
        files.append(os.path.join(os.getcwd(), file))

for x in files:
    item = QtGui.QListWidgetItem()
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(_fromUtf8(x)), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    item.setIcon(icon)
    self.listWidget.addItem(item)
like image 186
thecreator232 Avatar answered Sep 30 '22 15:09

thecreator232