tablewidget单元格插入控件



import sys
from PySide2.QtWidgets import (QWidget, QTableWidget, QHBoxLayout, QApplication, QTableWidgetItem, QAbstractItemView,QComboBox, QPushButton)


class Table(QWidget):
    def __init__(self):
        super(Table, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle(u"QTableWidget 例子u")
        self.resize(430, 300)
        conLayout = QHBoxLayout()  # 创建水平布局文件
        tableWidget = QTableWidget()  # 创建一个列表
        tableWidget.setRowCount(4)  # 设置行数
        tableWidget.setColumnCount(3)  # 设置列数
        conLayout.addWidget(tableWidget)  # 添加列表到布局

        tableWidget.setHorizontalHeaderLabels([u'姓名u', u'性别u', u'体重(kg)u'])  # 设置水平表头

        newItem = QTableWidgetItem(u"张三u")  # 添加张三 到(0,0)
        tableWidget.setItem(0, 0, newItem)

        comBox = QComboBox()  # 新建一个下拉组件
        comBox.addItem(u"男u")
        comBox.addItem(u"女u")
        comBox.setStyleSheet(u"QComboBox{margin:3px};u")
        comBox.currentIndexChanged.connect(self.comboxSelect) #绑定combox select 事件
        tableWidget.setCellWidget(0, 1, comBox)  # 添加下拉组件到列表(0,1)

        searchBtn = QPushButton(u"修改u")  # 新建一个按钮
        searchBtn.setDown(True)
        searchBtn.setStyleSheet(u"QPushButton{margin:3px};u")
        searchBtn.clicked.connect(self.butClick) #绑定按钮点击事件
        tableWidget.setCellWidget(0, 2, searchBtn)  # 添加按钮到列表(0,2)

        self.setLayout(conLayout)

    def comboxSelect(self,index):
        print(u"combox select indexu",index)

    def butClick(self):
        print(u"button clicku")


example = Table()
example.show()




评论
  目录