带有一列图像的 QTableView
我有一个 QTableView 以网格的形式显示数据库的一些信息。其中一个字段是图像的路径,我想在我的表格中显示这些图像。
我和代表一起尝试了一些东西,但我对他们不太满意,而且我无法让任何事情发挥作用。我还尝试了一些与角色有关的东西:
if index.column() == 4:
if role == QtCore.Qt.DecorationRole:
label = QtGui.QLabel()
path = "path/to/my/picture.jpg"
image = QtGui.QImage(str(path))
pixmap = QtGui.QPixmap.fromImage(image)
label.setPixmap(pixmap)
return label
这段代码的灵感来自于我在另一个论坛中发现的东西,并且应该可以工作。然而它对我没有任何作用,只会减慢我的代码的执行速度。
知道为什么它不起作用吗?如果您有一个代表的例子,我也将不胜感激!
感谢您的关注
已解决: 我让它与自定义代表一起工作。如果有人感兴趣,这是我的代码:
class ImageDelegate(QtGui.QStyledItemDelegate):
def __init__(self, parent):
QtGui.QStyledItemDelegate.__init__(self, parent)
def paint(self, painter, option, index):
painter.fillRect(option.rect, QtGui.QColor(191,222,185))
# path = "path\to\my\image.jpg"
path = "araignee_de_mer.jpg"
image = QtGui.QImage(str(path))
pixmap = QtGui.QPixmap.fromImage(image)
pixmap.scaled(50, 40, QtCore.Qt.KeepAspectRatio)
painter.drawPixmap(option.rect, pixmap)
I have a QTableView to display some informations of a database in the form of a grid. One of the fields is a path to an image and I would like to display these images in my table.
I tried something with a delegate, but I'm not really confortable with them and I couldn't get anything working. I also tried something with the role :
if index.column() == 4:
if role == QtCore.Qt.DecorationRole:
label = QtGui.QLabel()
path = "path/to/my/picture.jpg"
image = QtGui.QImage(str(path))
pixmap = QtGui.QPixmap.fromImage(image)
label.setPixmap(pixmap)
return label
This piece of code is inspired by something I found in another forum and that was supposed to work. However it doesn't do anything for me, only slows down the execution of my code.
Any idea why it's not working ? If you have an example with a delegate I'd appreciate it also!
Thanks for your attention
RESOLVED:
I got it working with a custom delegate. Here is my code if someone's interested :
class ImageDelegate(QtGui.QStyledItemDelegate):
def __init__(self, parent):
QtGui.QStyledItemDelegate.__init__(self, parent)
def paint(self, painter, option, index):
painter.fillRect(option.rect, QtGui.QColor(191,222,185))
# path = "path\to\my\image.jpg"
path = "araignee_de_mer.jpg"
image = QtGui.QImage(str(path))
pixmap = QtGui.QPixmap.fromImage(image)
pixmap.scaled(50, 40, QtCore.Qt.KeepAspectRatio)
painter.drawPixmap(option.rect, pixmap)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您提供解决方案和代码示例,Johanna。这是一个巨大的帮助。
另外,如果其他人需要像我一样拼写出来:
1)为 QTableView 的图像承载列设置项目委托(我在 myTableView.init() 中这样做),如下所示:
2)在 yourImageDelegate 类中,您可能需要重载 sizeHint() 以适应图像的大小,如下所示:
Swoot!
Thank you for the solution and the code example, Johanna. It was a huge help.
In addition, in case anyone else needs it spelled out like I did:
1) Set the item delegate for the image-bearing column of your QTableView (I did this in myTableView.init()) like so:
2) In yourImageDelegate class, you may want to overload sizeHint() for the size of your image like so:
Swoot!
当我测试时,我使用drawPixmap(option.rect.x(), option.rect.y(), pixmap)来绘制图标,不需要做pixmap.scaled。
when i test , i use drawPixmap(option.rect.x(), option.rect.y(), pixmap) to draw icon, noneed do pixmap.scaled.
我认为您不需要将像素图封装在标签或图像中。尝试只返回像素图。
I don't think you need to encapsulate the Pixmap in a Label or an Image. Try just returning the Pixmap.