网络编程
位置:首页>> 网络编程>> Python编程>> PyQt5中向单元格添加控件的方法示例

PyQt5中向单元格添加控件的方法示例

作者:放大的EZ  发布时间:2023-10-20 05:08:48 

标签:PyQt5,单元格,控件

1、简介

pyqt 列表 单元格中 不仅可以添加数据,还可以添加控件。

我们尝试添加下拉列表、一个按钮试试。

PyQt5中向单元格添加控件的方法示例

setItem:将文本放到单元格中
setCellWidget:将控件放到单元格中
setStyleSheet:设置控件的样式(Qt StyleSheet)

2、功能实现


# -*- coding: utf-8 -*-

'''
【简介】
PyQT5中 单元格里面放控件

'''

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

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

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

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

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

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

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

self.setLayout(conLayout)

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

def butClick(self):
 print("button click")

if __name__ == '__main__':
app = QApplication(sys.argv)
example = Table()
example.show()
sys.exit(app.exec_())

文件参考:PyQt 快速开发与实践

来源:https://blog.csdn.net/qq_27061049/article/details/89875503

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com