带有一列图像的 QTableView

发布于 2024-11-16 17:48:22 字数 1192 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蹲在坟头点根烟 2024-11-23 17:48:22

感谢您提供解决方案和代码示例,Johanna。这是一个巨大的帮助。

另外,如果其他人需要像我一样拼写出来:

1)为 QTableView 的图像承载列设置项目委托(我在 myTableView.init() 中这样做),如下所示:

self.setItemDelegateForColumn(1, yourImageDelegate(parent))

2)在 yourImageDelegate 类中,您可能需要重载 sizeHint() 以适应图像的大小,如下所示:

def sizeHint(self, option, index) :
    return QSize(160, 90) # whatever your dimensions are

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:

self.setItemDelegateForColumn(1, yourImageDelegate(parent))

2) In yourImageDelegate class, you may want to overload sizeHint() for the size of your image like so:

def sizeHint(self, option, index) :
    return QSize(160, 90) # whatever your dimensions are

Swoot!

窝囊感情。 2024-11-23 17:48:22

当我测试时,我使用drawPixmap(option.rect.x(), option.rect.y(), pixmap)来绘制图标,不需要做pixmap.scaled。

class ImageDelegate(QtGui.QStyledItemDelegate):
    def __init__(self, parent=None):
        QtGui.QStyledItemDelegate.__init__(self, parent)
        #self.icon =icon
    def paint(self, painter, option, index):
        #painter.fillRect(option.rect, QtGui.QColor(191,222,185))
        # path = "path\to\my\image.jpg"
        path = "icon1.png"
        image = QtGui.QImage(str(path))
        pixmap = QtGui.QPixmap.fromImage(image)
        #pixmap.scaled(16, 16, QtCore.Qt.KeepAspectRatio)
        # when i test ,just use option.rect.x(), option.rect.y(), no need scaled 
        painter.drawPixmap(option.rect.x(), option.rect.y(),  pixmap)

when i test , i use drawPixmap(option.rect.x(), option.rect.y(), pixmap) to draw icon, noneed do pixmap.scaled.

class ImageDelegate(QtGui.QStyledItemDelegate):
    def __init__(self, parent=None):
        QtGui.QStyledItemDelegate.__init__(self, parent)
        #self.icon =icon
    def paint(self, painter, option, index):
        #painter.fillRect(option.rect, QtGui.QColor(191,222,185))
        # path = "path\to\my\image.jpg"
        path = "icon1.png"
        image = QtGui.QImage(str(path))
        pixmap = QtGui.QPixmap.fromImage(image)
        #pixmap.scaled(16, 16, QtCore.Qt.KeepAspectRatio)
        # when i test ,just use option.rect.x(), option.rect.y(), no need scaled 
        painter.drawPixmap(option.rect.x(), option.rect.y(),  pixmap)
望笑 2024-11-23 17:48:22

我认为您不需要将像素图封装在标签或图像中。尝试只返回像素图。

if index.column() == 4:
    if role == QtCore.Qt.DecorationRole:            
        path = "path/to/my/picture.jpg"
        pixmap = QtGui.QPixmap(path) 
        return pixmap

I don't think you need to encapsulate the Pixmap in a Label or an Image. Try just returning the Pixmap.

if index.column() == 4:
    if role == QtCore.Qt.DecorationRole:            
        path = "path/to/my/picture.jpg"
        pixmap = QtGui.QPixmap(path) 
        return pixmap
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文