在 QTableWidget 表格中实现图文混排效果 的案例
改变单元格中图片的尺寸
setIconSize(QSize(width,height)
import sys
from PyQt5 import QtPrintSupport, QtGui
from PyQt5.QtCore import Qt, QMimeData, QDate, QDateTime, QTime, QStringListModel, QSize
from PyQt5.QtGui import QIcon, QPainter, QBrush, QPixmap, QStandardItemModel, QStandardItem, QColor, QFont
from PyQt5.QtPrintSupport import QPageSetupDialog, QPrinter, QPrintDialog
from PyQt5.QtWidgets import QApplication, QWidget, QComboBox, QFormLayout, QLabel, QLineEdit, QPushButton, QGridLayout, \
QCalendarWidget, QVBoxLayout, QDateTimeEdit, QAction, QMainWindow, QTextEdit, QStatusBar, QFileDialog, QDialog, \
QTableView, QMessageBox, QListView, QListWidget, QHBoxLayout, QTableWidget, QTableWidgetItem, QAbstractItemView
'''
在 QTableWidget 表格中实现图文混排效果 的案例
改变单元格中图片的尺寸
setIconSize(QSize(width,height)
'''
class CellImageTextDemo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置定位和左上角坐标
self.setGeometry(300, 300, 460, 360)
# 设置窗口标题
self.setWindowTitle('QTableWidget扩展表格的实现图文混排效果,改变片尺寸 的演示')
# 设置窗口图标
# self.setWindowIcon(QIcon('../web.ico'))
layout = QVBoxLayout()
self.tablewidget = QTableWidget()
self.tablewidget.setRowCount(3)
self.tablewidget.setColumnCount(4)
layout.addWidget(self.tablewidget)
self.tablewidget.setHorizontalHeaderLabels(['姓名','性别','体重(kg)','显示图片'])
newItem = QTableWidgetItem('美国队长')
self.tablewidget.setItem(0,0,newItem)
newItem = QTableWidgetItem('男')
self.tablewidget.setItem(0,1,newItem)
newItem = QTableWidgetItem('180')
self.tablewidget.setItem(0,2,newItem)
# 设置图片大小
self.tablewidget.setIconSize(QSize(300,200))
# 让列的宽度和图片的宽度相同
# for i in range(3):
# self.tablewidget.setColumnWidth(i,300)
# 让行的高度和图片的高度相同
# for i in range(3):
# self.tablewidget.setRowHeight(i,200)
# 设置宽和高
self.tablewidget.setColumnWidth(3, 300)
self.tablewidget.setRowHeight(0, 200)
newItem = QTableWidgetItem(QIcon('../1.jpg'),'180')
self.tablewidget.setItem(0,3,newItem)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置应用图标
app.setWindowIcon(QIcon('../web.ico'))
w = CellImageTextDemo()
w.show()
sys.exit(app.exec_())