Qtablewidge添加窗口控件例子
时间: 2023-07-19 20:18:16 浏览: 129
以下是使用QTableWidget添加窗口控件的示例代码:
```python
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QComboBox, QPushButton, QWidget, QHBoxLayout, QVBoxLayout
from PyQt5.QtCore import Qt
class MyTableWidget(QTableWidget):
def __init__(self, parent):
super().__init__(parent)
# 设置表格大小和列数
self.setRowCount(5)
self.setColumnCount(3)
# 添加控件
for i in range(self.rowCount()):
# 添加下拉框
combo_box = QComboBox()
combo_box.addItem("选项1")
combo_box.addItem("选项2")
combo_box.addItem("选项3")
self.setCellWidget(i, 0, combo_box)
# 添加按钮
button = QPushButton("按钮")
self.setCellWidget(i, 1, button)
# 添加文本框
item = QTableWidgetItem("文本框")
self.setItem(i, 2, item)
if __name__ == '__main__':
app = QApplication([])
widget = QWidget()
layout = QVBoxLayout(widget)
table = MyTableWidget(widget)
layout.addWidget(table)
widget.show()
app.exec_()
```
在这个例子中,我们创建了一个继承自QTableWidget的自定义类MyTableWidget。在构造函数中,我们设置了表格的行数和列数,并使用setCellWidget和setItem方法向表格中添加了控件。其中,第一列添加了一个下拉框,第二列添加了一个按钮,第三列添加了一个文本框。
最后,我们将这个自定义表格控件添加到一个QWidget中,并将QWidget显示出来。
阅读全文
相关推荐













