【问题标题】:Adding images to a QTableWidget in PyQt在 PyQt 中将图像添加到 QTableWidget
【发布时间】:2011-07-30 00:40:52
【问题描述】:

我对 Python 非常陌生,对 PyQt 甚至更陌生。我设法创建了一个表格,但想在某些单元格中添加图像。我读过我需要继承 QTableWidget 类,或者可能是 QTableWidgetItem 类并重新实现 QPaintEvent。如果有人有重新实现 QPaintEvent 的示例,我将不胜感激。

谢谢, 斯蒂芬

【问题讨论】:

    标签: python pyqt qtablewidget qtablewidgetitem


    【解决方案1】:
    from PyQt4 import QtGui
    import sys
    
    imagePath = "enter the path to your image here"
    
    class ImgWidget1(QtGui.QLabel):
    
        def __init__(self, parent=None):
            super(ImgWidget1, self).__init__(parent)
            pic = QtGui.QPixmap(imagePath)
            self.setPixmap(pic)
    
    class ImgWidget2(QtGui.QWidget):
    
        def __init__(self, parent=None):
            super(ImgWidget2, self).__init__(parent)
            self.pic = QtGui.QPixmap(imagePath)
    
        def paintEvent(self, event):
            painter = QtGui.QPainter(self)
            painter.drawPixmap(0, 0, self.pic)
    
    
    class Widget(QtGui.QWidget):
    
        def __init__(self):
            super(Widget, self).__init__()
            tableWidget = QtGui.QTableWidget(10, 2, self)
            tableWidget.setCellWidget(0, 1, ImgWidget1(self))
            tableWidget.setCellWidget(1, 1, ImgWidget2(self))
    
    if __name__ == "__main__":
        app = QtGui.QApplication([])
        wnd = Widget()
        wnd.show()
        sys.exit(app.exec_())
    

    两种方法,因为您要求使用 painEvent。

    致谢:http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg01259.html

    希望这会有所帮助。

    编辑:使用 QTableWidget 子类添加了请求的解决方案。

    from PyQt4 import QtGui
    import sys
    
    class ImageWidget(QtGui.QWidget):
    
        def __init__(self, imagePath, parent):
            super(ImageWidget, self).__init__(parent)
            self.picture = QtGui.QPixmap(imagePath)
    
        def paintEvent(self, event):
            painter = QtGui.QPainter(self)
            painter.drawPixmap(0, 0, self.picture)
    
    
    class TableWidget(QtGui.QTableWidget):
    
        def setImage(self, row, col, imagePath):
            image = ImageWidget(imagePath, self)
            self.setCellWidget(row, col, image)
    
    if __name__ == "__main__":
        app = QtGui.QApplication([])
        tableWidget = TableWidget(10, 2)
        tableWidget.setImage(0, 1, "<your image path here>")
        tableWidget.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 谢谢,帮了大忙。是否也可以继承 QTableWidget 类以创建类似:tableWidget.setImage(imagePath)?或者这会花费这么多努力以至于不值得?
    • 当我看到它是黑白的时,它似乎很明显。谢谢
    • 很高兴为您提供帮助。如果有机会,请随时接受这个答案:)
    • 我希望这是一个可以快速简单地回答的问题。如果您要更改外观(单击单元格时会发生什么,标题的颜色和格式等),是否更容易修改 QTableWidget,或者使用 QTableView 和 QModel 创建自己的表格?
    • 恐怕我对 Qt 的模型/视图类没有太多经验。就我个人而言,我会看看我是否可以使用提供的小部件做我需要的事情。到目前为止,我可以。
    【解决方案2】:

    我发现这个解决方案对我的初学者很友好:

    from PyQt5 import QtCore, QtGui, QtWidgets
    import sys
    
    class KindForMind(object):
        def THINK(self, PLEASE):
            self.table = QtWidgets.QTableWidget()
            pic = QtGui.QPixmap("your_image.jpg")
            self.label = QtWidgets.QLabel(PLEASE)
            self.label.setPixmap(pic)
            self.table.setCellWidget(0,0, self.label)
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        PLEASE = QtWidgets.QWidget()
        ui = KindForMind()
        ui.THINK(PLEASE)
        PLEASE.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 2012-08-14
      • 1970-01-01
      • 2017-05-10
      • 2011-07-31
      • 2017-01-16
      • 2022-11-24
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多