其他分享
首页 > 其他分享> > PyQt5基础学习-QTableWidgetItem(QIcon(path), "文字")(表格中元素实现图文并排)

PyQt5基础学习-QTableWidgetItem(QIcon(path), "文字")(表格中元素实现图文并排)

作者:互联网

使用QTableWidgetItem(图片, 文字)实现图文并排

CellImageText.py

"""
在单元格中实现图文混排的功能
"""
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class CellImageText(QWidget):

    def __init__(self):
        super(CellImageText, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("在单元格中实现图文混排的效果")
        self.resize(500, 300)
        layout = QHBoxLayout()
        self.tableWidget = QTableWidget()
        self.tableWidget.setRowCount(5)
        self.tableWidget.setColumnCount(4)
        layout.addWidget(self.tableWidget)

        self.tableWidget.setHorizontalHeaderLabels(['姓名', '性别', '体重', '显示图片'])

        newItem = QTableWidgetItem("李宁")
        self.tableWidget.setItem(0, 0, newItem)

        newItem = QTableWidgetItem("李宁")
        self.tableWidget.setItem(0, 0, newItem)

        newItem = QTableWidgetItem("男")
        self.tableWidget.setItem(0, 1, newItem)

        newItem = QTableWidgetItem("160")
        self.tableWidget.setItem(0, 2, newItem)

        newItem = QTableWidgetItem(QIcon("picture/F6fhJr.jpg"), "风景")
        self.tableWidget.setItem(0, 3, newItem)
        self.setLayout(layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = CellImageText()
    main.show()

    sys.exit(app.exec_())

 

标签:__,self,setItem,PyQt5,QTableWidgetItem,tableWidget,newItem,QIcon
来源: https://www.cnblogs.com/hyq-lst/p/15863742.html