Question

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?

Was it helpful?

Solution

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)

OTHER TIPS

Not a copy-paste grade answer, but I don't see why it should not be possible:

  1. Get the image data
  2. Construct a QPixmap from the image data. You can go via a QImage as an intermediate step and then e.g. use QPixmap.convertFromImage.
  3. Using any "list" kind of widget you could use a QStandardItemModel, setting the item's Icon to a QIcon(pixmap) with the pixmap you created in step 2.

Check the qt/Pyqt class reference on the mentioned classes:

http://qt-project.org/doc/qt-4.8/classes.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top