在 PYQT 中用图像填充列表 [英] Populating list with images in PYQT

查看:37
本文介绍了在 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?

推荐答案

好的 ListView 和 ListWidget 都允许用户以列表模式或图标模式显示内容.因此您可以将视图模式设置为图标模式并在列表视图中显示图像.

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)

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)

这是这个的输出.

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)

这篇关于在 PYQT 中用图像填充列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆