【问题标题】:QTableView with a column of images带有一列图像的 QTableView
【发布时间】:2011-09-21 19:49:47
【问题描述】:

我有一个 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) 

【问题讨论】:

    标签: python image pyqt qtableview


    【解决方案1】:

    感谢 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
    

    扑通一声!

    【讨论】:

    • 如果我每行有多个图像路径,如何将路径传递给委托?谢谢
    【解决方案2】:

    当我测试时,我使用 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)
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 感谢您的关注,但这似乎没有任何区别。我认为它会计算像素图,因为网格需要几秒钟来加载,而且通常是立即的。但它不显示任何内容
      • 我怀疑问题出在代码的其他部分。你确定你有正确的列?
      • 我终于让它与自定义委托一起工作。在我的问题中发布我的代码
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多